Thursday, 16 January 2020

Why does Firebase pendingAuthResult return null?

I am trying to implement Sign in with Apple using Firebase Authentication. I am following the firebase/quickstart-android sample.

My sign-in fragment overrides onStart() to check for any pending results:

override fun onStart() {
    super.onStart()

    val pending = auth.pendingAuthResult

    pending?.addOnSuccessListener { authResult ->
        Timber.d("Successful login, pending")
    }?.addOnFailureListener { e ->
        Timber.d("Failed login, pending")
    }
}

And a button that initiates the sign-in flow:

btnApple.onClick {
    viewModel.appleLogin(requireActivity())
}

The viewModel calls the following method from a repository:

// Initiate sign-in flow only if there are no pending results
if (auth.pendingAuthResult != null) {
    return
}

val scopes = listOf("email", "name")
val provider = OAuthProvider.newBuilder("apple.com", auth)
    .setScopes(scopes)
    .build()

auth.startActivityForSignInWithProvider(activity, provider)
    .addOnSuccessListener { authResult ->
        Timber.d("Successful login, normal")
    }
    .addOnFailureListener { e ->
        Timber.e(e, "Failed login, normal")
    }

The official manual states:

Signing in with this method puts your Activity in the background, which means that it can be reclaimed by the system during the sign in flow.

So I started testing the pending result by terminating the app in Android Studio while completing the sign-in flow in Chrome. Once I returned back to the app, the onStart() was called, but the pendingAuthResult was always null.

To make it more interesting, when I restart the app, I am logged in. Then if I log out and enter the sign-in fragment again, there is a pending result now and I receive Successful login, pending. On top of that, the pending result does not disappear. If I leave the sign-in fragment and go back, the pending result is still there and I receive yet another Successful login, pending.

I even tested the firebase/quickstart-android sample itself and it has exactly the same issue.

What could be the possible cause of this issue? I am using firebase-auth:19.2.0.



from Why does Firebase pendingAuthResult return null?

No comments:

Post a Comment