How to get Firebase idToken from AuthResult?

江枫思渺然 提交于 2021-02-11 15:27:33

问题


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).


回答1:


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.



来源:https://stackoverflow.com/questions/60268077/how-to-get-firebase-idtoken-from-authresult

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!