Home > Software engineering >  AuthStateListener callback is not triggering after reloading the firebase user on Android
AuthStateListener callback is not triggering after reloading the firebase user on Android

Time:01-14

I'm having a little issue with FirebaseAuth.AuthStateListener while working with email verification on Firebase. I've verified my email by clicking the received verification link, and then I reloaded the current user by the lines of code below.

suspend fun reloadUserInfo() {
    firebaseAuth.currentUser?.reload()?.await()
}

But AuthStateListener is not firing up even tho I reloaded the cached user. If I understood correctly AuthStateListener should trigger after reloading the current user. The reload() function's documentation says: Manually refreshes the data of the current user (for example, attached providers, display name, and so on). The isEmailVerified state changed of the firebase user. Right?

val isEmailVerified: Flow<Boolean> = callbackFlow {
    val authStateListener = AuthStateListener { auth ->
        val isEmailVerified = auth.currentUser?.isEmailVerified == true
        trySend(isEmailVerified)
    }
    firebaseAuth.addAuthStateListener(authStateListener)
    awaitClose {
        firebaseAuth.removeAuthStateListener(authStateListener)
    }
}

This flow is not sending anything. But after restarting the application the callback gets fired. I don't want to restart the application to get the job done. It would not be a good user experience.

I did some research but nothing was found. If you take the time to help me, I appreciate it. Thanks!

CodePudding user response:

The issue you are encountering is likely due to the fact that the AuthStateListener is only triggered when the user's authentication state changes, not when the user's profile information changes. When you call the reload() method, it updates the user's profile information, but it does not change their authentication state, so the AuthStateListener is not fired.

One way to solve this issue is to use a combination of the reload() method and the AuthStateListener. You can call the reload() method to update the user's profile information, and then use the AuthStateListener to check if the email is verified after the reload is complete.You could try to use a callback when the reload() method is completed, like this:

suspend fun reloadUserInfo() {
firebaseAuth.currentUser?.reload()?.addOnCompleteListener {
    if(it.isSuccessful) {
        // reload successfully
        // check if the email is verified
        if(firebaseAuth.currentUser?.isEmailVerified == true) {
            // email is verified, do something
        }
    } else {
        // reload failed
        // handle the error
    }
}?.await()

}

This way you would have the reload method and the check for the email verification in the same function.

Alternatively, you could use a Timer function to regularly check if the email is verified after the reload method is called.

It is also worth noting that the reload() method returns a Task object, so you can use the addOnCompleteListener() method to be notified when the reload is complete

  • Related