androidfirebaseexceptionfirebase-authenticationandroid-task

Firebase Authentication: how to verify user still exists


I am using Firebase Anonymous Authentication.

In case a user has been deleted from the Firebase console, the client still believes the user exists, as it still holds the token.

Each time I start the app, I would like to verify if the user still exists. I found out I can use the FirebaseUser.reload() function.

The documentation says that in case the user's account has been disabled or deleted, the FirebaseAuthInvalidUserException will be thrown: https://firebase.google.com/docs/reference/android/com/google/firebase/auth/FirebaseUser.html#reload()

However this is an asyncronous function, and I am struggling finding out how to catch this exception. Can anyone show me a code sample on how to catch this exception? I have looked all documentation, but I haven't found a sample about this.


Solution

  • Try this:

    mFirebaseUser.reload().addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            if (e instanceof FirebaseAuthInvalidUserException) {
                Log.d(TheApp.LOG_TAG, "user doesn't exist anymore");
                createUser();
            }
        }
    });