3
votes

The Firebase Auth doc's recommended way to get the current user is:

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
    // User is signed in
} else {
    // No user is signed in
}

So I went ahead and did exactly that at the very beginning of my first activity:

override fun onCreate(savedInstanceState: Bundle?) {
        Log.d(TAG, "⟳ onCreate")
        super.onCreate(savedInstanceState)

        if (FirebaseAuth.getInstance().currentUser == null) {
            Log.d(TAG, "User is null. Sending user to log in.")
            val intent = Intent(this, FacebookLoginActivity::class.java)
            startActivity(intent)
            finish()
        } else {
            Log.d(TAG, "User has been found. Launching MainActivity")
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
            finish()
        }
    }

But this is when strange things start happening ????

Steps:

  1. I killed the app from memory
  2. Deleted all its data & cache
  3. Uninstall & install again then launch

    Actual: FirebaseAuth.getInstance().currentUser was NOT null! Okay guessing the Firebase library is trying to do something smart and figure out my previous log in history, but:

    • The uid it returns is not my user Id. I cannot use this as I already keep data for the user in my DB with an other legit uid previously returned for this user.
    • I can NOT find this uid on https://console.firebase.google.com/u/0/project/.../authentication/users How is this possible?
    • This uid has been popping up at various times causing me errors, and it is always this same rogue uid.

A couple of things I checked:

  • FirebaseAuth.getInstance().currentUser.providerData shows that this uid is associated with my Facebook account, so I must have logged in with it before. (I call FacebookAuthProvider.getCredential(...) in the next activity) But my real legit uid should also be associated with my Facebook account which is the one I am interested in.

  • FirebaseAuth.getInstance().currentUser.isAnonymous returns false, so the problem is probably not related anonymous login

How do I clear this "phantom" rogue user id from the system? How do I check for this? I don't want to call FacebookAuthProvider.getCredential(...) every time the user opens the app.

(Using 'firebase-auth:12.0.1')

2
Are you also deleting the user from the Firebase Console? - Alex Mamo
@AlexMamo Yes sometimes I do delete users on the console while testing. I cannot say for sure wether I ever deleted this user. - Crocodile

2 Answers

0
votes

If you have a user that is apart of your app and you if you decide to delete its account from the Firebase Console, you need to know that if the same returns to use your app again, another uid is generated, which is obvious that is different from the first one. So this user, even if it has the same details (userName, emailAddress and so on) before it was deleted, is treated as a new user, with a new uid.

0
votes

I got the same issue as you. I am using flutter with Android device. This is what I tried:

  1. Clear the cache for the app.
  2. Delete the app.
  3. Restart the phone.
  4. Reinstall the app.

Then, FirebaseAuth.getInstance().getCurrentUser() will no longer hold the value.

For example in my app, I use firestore, it will throw permission error when accessing firestore resource because of invalid userId (it dependes on the security rules). You can make your app defensive to ask user to login again. Or you can do firebaseUser.reload() it will throw userId invalid exception.

The actually reason why FirebaseAuth.getInstance().getCurrentUser() will hold the value before reboot still unknown.