android – How to get Firebase idToken from AuthResult?-ThrowExceptions

Exception or error:

Firebase are providing AuthResult on the callback for user sign in using startActivityForSignInWithProvider (in my case the provider is apple on Android platform).
I can’t seem to fetch the idToken from it in a proper (synchronous) way.

So far I found two options, both aren’t satisfying:

Option 1: casting

((OAuthCredential)authResult.getCredential()).getIdToken()

This works but I’m not sure if it’s a safe cast. Plis, I’m thinking, if Firebase would have wanted us to fetch the idToken this way, they would have added it to AuthCredential.

Option 2: fetching it from the user

authResult.getUser().getIdToken(false).addOnCompleteListener { ... }

This seems to be a safe solution, but it’s an async method. It doesn’t seem to make sense to performing another async method, after the user just manually signed in (this is what i’m doing for an existing user).

How to solve:

You should be using the asynchronous listener. The reason for this is because the ID token will expire every hour, and your listener is how you know the very moment when the token has been refreshed, so you can start using that new token right away.

Leave a Reply

Your email address will not be published. Required fields are marked *