Home » Android » android – How to get Firebase idToken from AuthResult?-Exceptionshub

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

Posted by: admin February 26, 2020 Leave a comment


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


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&Answers:

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.