英文:
Firebase Auth: How to sign in using id token?
问题
有没有一种方法可以在Firebase Auth中共享匿名用户会话?
我想要的是获取当前的ID令牌:
final idToken = FirebaseAuth.instance.currentUser!.getIdToken();
然后,使用这个idToken
在不同的应用程序中对同一匿名用户进行身份验证。
FirebaseAuth.instance.signInWithToken(idToken);
英文:
Is there a way to share anonymous user sessions with Firebase Auth?
What I would like to is to get the current id token:
final idToken = FirebaseAuth.instance.currentUser!.getIdToken();
Then, use this idToken
to authenticate the same anonymous user in a different app.
FirebaseAuth.instance.signInWithToken(idToken);
答案1
得分: 0
使用signInWithCustomToken()
方法,您可以使用自定义身份验证令牌在不同的网站上登录用户。如此处所述:
Firebase通过允许您使用安全的JSON Web令牌(JWT)对用户或设备进行身份验证,为您提供完全的身份验证控制。您在服务器上生成这些令牌,将它们传递回客户端设备,然后使用它们进行身份验证,方法是使用signInWithCustomToken()
。
您可以使用Firebase Admin SDK创建自定义令牌,或者如果您的服务器使用Firebase不原生支持的语言编写,可以使用第三方JWT库。
Firebase Admin SDK具有用于创建自定义令牌的内置方法。至少,您需要提供一个uid,它可以是任何字符串,但应唯一标识您要进行身份验证的用户或设备。这些令牌在一小时后过期。
创建自定义令牌后,您应该将其发送到您的客户端应用程序。客户端应用程序通过调用signInWithCustomToken()
来使用自定义令牌进行身份验证。
还可以查看以下链接以获取更多信息和示例:
如何在Flutter应用程序中使用相同的Firebase匿名用户
英文:
With signInWithCustomToken()
method, you can use a custom auth token to sign in a user on different website. As documented here
> Firebase gives you complete control over authentication by allowing
> you to authenticate users or devices using secure JSON Web Tokens
> (JWTs). You generate these tokens on your server, pass them back to a
> client device, and then use them to authenticate via the
> signInWithCustomToken()
method.
>
> You can create a custom token with the Firebase Admin SDK, or you can
> use a third-party JWT library if your server is written in a language
> which Firebase does not natively support.
>
> The Firebase Admin SDK has a built-in method for creating custom
> tokens. At a minimum, you need to provide a uid, which can be any
> string but should uniquely identify the user or device you are
> authenticating. These tokens expire after one hour.
>
> After you create a custom token, you should send it to your client
> app. The client app authenticates with the custom token by calling
> signInWithCustomToken()
Also check out these links for more information and examples:
Authenticate with Firebase Using a Custom Authentication System
Firebase auth - login user from app in website
How to use the same firebase anonymous user in a flutter app
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论