Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android FirebaseAuth.getCurrentUser() Never Null

I have a DispatchActivity as my Launcher Activity, which is meant to check if there is a user currently signed in. If the user is signed in, I send them to their ProfileActivity. Otherwise, I send them to a LogInActivity. Here is my code in DispatchActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dispatch);

    //.....................

    auth = FirebaseAuth.getInstance();

    authListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if (user != null) {
                launchProfileActivity();
            } else {
                // User is signed out
                launchLoginActivity();
            }
        }
    };
}

@Override
public void onStart() {
    super.onStart();
    auth.addAuthStateListener(authListener);
}

@Override
public void onStop() {
    super.onStop();
    if (authListener != null) {
       auth.removeAuthStateListener(authListener);
    }
}

No matter what I do, firebaseAuth.getCurrentUser(), never returns null on my primary testing device. According to the Docs, getCurrentUser() will be null unless there is a User Currently Signed in. When I Log some of this User's details, they are consistent with a "Test" user I created previously, (i.e. calling user.getEmail() returns "[email protected]".

This is problematic, as I most certainly don't have any users in my Console's User Database (In Authentication/Users). I deleted this test User from the database some time ago, and the database is empty.

I tried running the App on another device, and it executed properly. However, performing a fresh install on my primary testing device does not fix the problem.

Question: As far as I can see, the issue is related to some kind of persistent user state with my device; since running a different device works fine. Since I haven't deliberately configured this to occur, I would like to know what is causing this inconsistency between my Auth User Database and the Device.

If the Database is empty, where is auth.getCurrentUser() getting this previously deleted user object from?

Thank you.

like image 853
Ryan Avatar asked Jan 22 '17 23:01

Ryan


1 Answers

Firebase Authentication will cache an authentication token for the user when they're signed in. This prevents having to authenticate every little interaction the user makes with other services provided by Firebase. This token gets automatically refreshed periodically, but until then, the SDK assumes that the token represents the user. You'll find that the token will expire after some time, or you can force the issue by uninstalling and reinstalling the app, or clearing its data.

Authentication works regardless of the contents of your Realtime Database. They are independent from each other, except where security rules limit access to the currently authenticated user.

like image 81
Doug Stevenson Avatar answered Nov 06 '22 02:11

Doug Stevenson