Ryan
Ryan

Reputation: 762

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.

Upvotes: 6

Views: 2787

Answers (3)

DragonFire
DragonFire

Reputation: 4082

In Your Manifest File Add The Following

In the <manifest> tag (at the end)

xmlns:tools="http://schemas.android.com/tools"

Then in the <application> tag (at the begining)

tools:replace="android:allowBackup"
android:allowBackup="false"
android:fullBackupContent="false"

just as @Doug Stevenson mentioned above firebase saves some data on the phone adding these tags will ensure that that does not happen... so when you uninstall the app and install it again it will clear all the data associated with the app.

Upvotes: 1

Daniel Kim
Daniel Kim

Reputation: 1

I had the same exact question and could not find the answer to this problem anywhere. I tried everything. I don't know why this works but I restarted my phone and it happened to fix everything. Super late but hope it helps for anyone else with the same problem.

Upvotes: 0

Doug Stevenson
Doug Stevenson

Reputation: 317352

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.

Upvotes: 4

Related Questions