Security [SOLVED]: Firebase only authenticating on some devices?

Security [SOLVED]: Firebase only authenticating on some devices?

Home Forums Security Security [SOLVED]: Firebase only authenticating on some devices?

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #36748

    Anonymous

    QuestionQuestion

    I have tested this on my Moto G5+ (works) and Nexus 6 (doesn’t work), and my firebase authentication only works on one of them:

      mAuth = FirebaseAuth.getInstance(); 
    
                if(mAuth.getCurrentUser() == null) { //No existing user
                    mAuth.signInAnonymously().addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Log.v("myTag", "Cannot authenticate user" + e);
                        }
                    });
                } 
                if(mAuth.getCurrentUser() != null) {
                    Log.v("myTag", "Boutta take photos");
    
                    takePhoto(this, 0);//back camera
                }else{
                    Log.v("myTag", "Cannot take photos, user not authenticated");
                }
    

    What’s odd is that I only get the log Cannot take photos, the user not authenticated, but not the log Cannot authenticate user. This means I am able to authenticate the user, but for some reason, it does not work

    How come this only works on some devices?

    #36749

    Anonymous

    Accepted AnswerAnswer

    Assuming your user starts off unauthenticated, once the first if statement is called and it attempts the anonymous sign in, that anonymous sign in happens asynchronously as it is waiting on a callback. So in that state the user isn’t authenticated until the call back completes. The code then jumps to your second set of if/else statements where you check

    mAuth.getCurrentUser() != null
    

    but the callback for authentication still may have not returned, and your mAuth.getCurrentUser() is still null therefore jumping to the else statement and logging the Log.v("myTag", "Cannot take photos, user not authenticated");

    Your callback may then return authenticating the user but at this point, its too late. You already run logic assuming the use wasn’t authenticated.

    Source: https://stackoverflow.com/questions/47958015/firebase-only-authenticating-on-some-devices
    Author: martinomburajr
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.