如何从AuthResult获取Firebase idToken? [英] How to get Firebase idToken from AuthResult?

查看:84
本文介绍了如何从AuthResult获取Firebase idToken?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Firebase在使用startActivityForSignInWithProvider的用户登录回调中提供AuthResult(在我的情况下,提供者是Android平台上的apple).我似乎无法以适当的(同步)方式从其获取idToken.

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:

选项1:投放

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

这有效,但是我不确定这是否安全.Plis,我在想,如果Firebase希望我们以这种方式获取idToken,他们会把它添加到AuthCredential中.

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.

选项2:从用户那里获取

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

推荐答案

您应该使用异步侦听器.这样做的原因是,ID令牌每小时都会过期,而您的侦听器将在刷新令牌的那一刻让您知道,因此您可以立即开始使用该新令牌.

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.

这篇关于如何从AuthResult获取Firebase idToken?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆