本文介绍了规则无法按预期工作的Firebase电子邮件身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 在管理屏幕中,我同时使用电子邮件认证和匿名登录。 数据只有一个节点 - Hello:World 我的规则是 rules:{$ b $.read:auth!= null, // \".read:true, .write:true } } 如果用户没有使用上述规则登录,那么您将获得读取失败:权限被拒绝这样很好。 如果一个匿名用户登录,那么我可以看到值{Hello = World} 到目前为止。 如果我使用电子邮件登录用户我得到一个错误:FirebaseError:权限被拒绝 onAuthenticate确实触发,我得到:用户ID:3c6ce912-a05a-49ed-ae68-8ec97d022303 ,Provider:password 完整的代码如下。我在做什么错了? import com.firebase.client.AuthData; import com.firebase.client.DataSnapshot; import com.firebase.client.Firebase; import com.firebase.client.FirebaseError; import com.firebase.client.ValueEventListener; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; public class FirebaseTest { Firebase ref = new Firebase(https://< FIREBASE NAME> .firebaseio.com /); public FirebaseTest(){ // logonAnonymous(); //作品 logonEmail(); //权限被拒绝 showValues(); 尝试{ System.out.println(等待输入); System.in.read(); catch(IOException ex){ Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE,null,ex); private void logonAnonymous(){ ref.authAnonymously(new Firebase.AuthResultHandler(){ @Override public void onAuthenticated(AuthData authData){ //我们已经使用您的Firebase应用程序对此会话进行了身份验证 System.out.println(User ID:+ authData.getUid()+,Provider: + $ authData.getProvider()); } $ b $ @覆盖 public void onAuthenticationError(firebaseError firebaseError){ //出现错误系统.out.println(Anon user auth error+ firebaseError.toString()); } }); private void logonEmail(){ ref.authWithPassword([email protected],secret,new Firebase.AuthResultHandler(){ @Override $ b $ public void onAuthenticated(AuthData authData){ System.out.println(User ID:+ authData.getUid()+,Provider:+ authData.getProvider )); } @Override public void onAuthenticationError(FirebaseError firebaseError){ //出现错误} } ); $ b $ public void showValues(){ ref.addValueEventListener(new ValueEventListener(){ @Override public void onDataChange DataSnapshot快照){ System.out.println(value - + snapshot.getValue()); } $ b @覆盖 public void onCancelled(FirebaseError firebaseError){ System.out.println(The read failed:+ firebaseError.getMessage()); } }); $ / code> } 解决方案我很肯定你只是看到电子邮件+密码认证异步发生的效果。 序列: logonEmail(); showValues(); 身份验证在 showValues() code $ c>执行。 异步排序的解决方案很简单,但最初非常不直观:您需要确保 showValues()只在认证成功后执行 一个简单的方法: private void logonEmail(){ ref.authWithPassword([email protected],secret,新的Firebase。 AuthResultHandler(){ @Override public void onAuthenticated(AuthData authData){ System.out.println(User ID:+ authData.getUid()+,Provider:+ authData.getProvider()); //用户已被认证,所以我们现在可以安全地调用showValue() showValues(); } @Override public void onAuthenticationError(firebaseError firebaseError){ //有一个错误} }); } In the management screen I have both email auth and anonymous login enabled.The data has only one node - Hello: "World"The rule that I have is { "rules": { ".read": "auth != null", //".read": true, ".write": true }}If the user is NOT logged in with the above rule then you get The read failed: Permission deniedThat's all good.If a anonymous user is logged in then I can see the value "{Hello=World"}Works so far.If i log in with a email user the I get an error :FirebaseError: Permission deniedThe onAuthenticate does trigger and I get :User ID: 3c6ce912-a05a-49ed-ae68-8ec97d022303, Provider: passwordThe complete code is below. What am I doing wrong?import com.firebase.client.AuthData;import com.firebase.client.DataSnapshot;import com.firebase.client.Firebase;import com.firebase.client.FirebaseError;import com.firebase.client.ValueEventListener;import java.io.IOException;import java.util.logging.Level;import java.util.logging.Logger;public class FirebaseTest {Firebase ref = new Firebase("https://<FIREBASE NAME>.firebaseio.com/");public FirebaseTest() { //logonAnonymous(); //works logonEmail(); //permission denied showValues(); try { System.out.println("Waiting for input"); System.in.read(); } catch (IOException ex) { Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex); }}private void logonAnonymous() { ref.authAnonymously(new Firebase.AuthResultHandler() { @Override public void onAuthenticated(AuthData authData) { // we've authenticated this session with your Firebase app System.out.println("User ID: " + authData.getUid() + ", Provider: " + authData.getProvider()); } @Override public void onAuthenticationError(FirebaseError firebaseError) { // there was an error System.out.println("Anon user auth error " + firebaseError.toString()); } });}private void logonEmail() { ref.authWithPassword("[email protected]", "secret", new Firebase.AuthResultHandler() { @Override public void onAuthenticated(AuthData authData) { System.out.println("User ID: " + authData.getUid() + ", Provider: " + authData.getProvider()); } @Override public void onAuthenticationError(FirebaseError firebaseError) { // there was an error } });}public void showValues() { ref.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot snapshot) { System.out.println( " value -" + snapshot.getValue()); } @Override public void onCancelled(FirebaseError firebaseError) { System.out.println("The read failed: " + firebaseError.getMessage()); } });}} 解决方案 I'm pretty sure you're just seeing the effects of the fact that email+password authentication happens asynchronously.In this sequence:logonEmail();showValues(); The authentication will not yet have finished by the time showValues() executes.The solution for asynchronous ordering is simple, but initially very unintuitive: you need to ensure that showValues() only executes after the authentication has succeeded.A simple way to do this:private void logonEmail() { ref.authWithPassword("[email protected]", "secret", new Firebase.AuthResultHandler() { @Override public void onAuthenticated(AuthData authData) { System.out.println("User ID: " + authData.getUid() + ", Provider: " + authData.getProvider()); // The user has been authenticated, so we can now safely call showValue() showValues(); } @Override public void onAuthenticationError(FirebaseError firebaseError) { // there was an error } });} 这篇关于规则无法按预期工作的Firebase电子邮件身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-23 23:01