Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase AuthInstance.currentUser is null after successfully reload

I'm currently having an issue during some of my users who log-in with email and password. The case is that after send the verification email, I'm calling reload in my current FirebaseUser to update the currentUser an know if the email have been verified or not.

Everything works fine at this point. The thing is that sometimes after call reload over an instance of FirebaseAuth.currentUser.reload(), on the successful callback of reload I'm trying to access again to the already updated FirebaseAuth.currentUser instance, and the funny thing is that this comes to be null and I'm getting a NPE, when the user have been successfully reloaded and the instance should be updated, not deleted.

This is my code right now:

 override fun verifyUser() {
        if (authInstance.currentUser == null) {
            dispatcher.dispatchOnUi(VerifyUserEmailCompleteAction(requestState = requestFailure(FirebaseUserNotFound()),
                    verified = false))
            return
        }
        RxFirebaseUser.reload(authInstance.currentUser!!)
                .subscribeOn(Schedulers.io())
                .subscribe({
                    dispatcher.dispatchOnUi(VerifyUserEmailCompleteAction(requestState = requestSuccess(),
                            verified = authInstance.currentUser!!.isEmailVerified))
                }, { error ->
                    dispatcher.dispatchOnUi(VerifyUserEmailCompleteAction(requestState = requestFailure(error),
                            verified = false))
                })
    }

The RxFirebase call is a Reactive wrapper over Firebase, but I have tried this code also using the normal Promise of the library and this error is happening too.

My NPE is coming on verified= authInstance.currentUser!!.isEmailVerified which should not be possible because I already did a check before start this call and the reload call have been successful.

Someone have suffer this issue and know why this could be happening? How can be the workaround for it? I could control the NPE there, but if the reload have been successful I want to update my user in my app data, not send a null instance.

like image 587
Francisco Durdin Garcia Avatar asked Apr 09 '26 11:04

Francisco Durdin Garcia


1 Answers

The authInstance.currentUser sometimes takes some time to be updated. You can move your reload call to the main thread on the observeOn to give authInstance more time to be updated, and also just in case add a retry clause to your code. It would be something like this:

RxFirebaseUser.reload(authInstance.currentUser!!)
            .subscribeOn(Schedulers.io())
            .repeatUntil { authInstance.currentUser != null }
            .defaultTimeout()
            .subscribe({
                val user = authInstance.currentUser
                val emailVerified = user?.isEmailVerified ?: false
                dispatcher.dispatchOnUi(VerifyUserEmailCompleteAction(requestState = if (user == null) requestRunning() else requestSuccess(),
                        verified = emailVerified))
            }, { error ->
                dispatcher.dispatchOnUi(VerifyUserEmailCompleteAction(requestState = requestFailure(error),
                        verified = false))
            })
like image 74
Adrián García Avatar answered Apr 11 '26 00:04

Adrián García



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!