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());
        }
    });
}

}

VladimirSD
  • 401
  • 3
  • 11

1 Answers1

3

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("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
        }
    });
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Also see Frank's other, [very informative, answer on async callbacks](http://stackoverflow.com/questions/27049342/asynchronous-access-to-an-array-in-firebase/27050749#27050749). – Kato Nov 09 '15 at 16:49