1

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 denied That'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 denied

The onAuthenticate does trigger and I get : User ID: 3c6ce912-a05a-49ed-ae68-8ec97d022303, Provider: password

The 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("myuser@home.com", "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());
        }
    });
}

}

4

1 回答 1

3

我很确定您只是看到了电子邮件+密码身份验证异步发生这一事实的影响。

在这个序列中:

logonEmail();
showValues();         

到执行时,身份验证还没有完成showValues()

异步排序的解决方案很简单,但最初非常不直观:您需要确保showValues()仅在身份验证成功后执行。

一个简单的方法来做到这一点:

private void logonEmail() {

    ref.authWithPassword("myuser@home.com", "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
        }
    });
}
于 2015-11-09T02:43:37.843 回答