Firebase-创建一个临时用户,直到该用户注册 [英] Firebase - create a temporary user until the user signs up

查看:125
本文介绍了Firebase-创建一个临时用户,直到该用户注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用例,用户A可以说用户B向用户A借了一些钱,类似于Splitwise之类的应用程序.

我正在使用Firestore存储数据.在这种情况下,我会将其作为文档存储在交易"集合中,该集合将包含以下字段:

金额:20
fromUser:用户A
的uid toUser:用户B的uid

这里的问题是用户B还不存在,因此用户B没有uid.我要做的是为用户B创建一个临时用户,其电子邮件地址将生成一个uid. .之后,当用户B注册该应用程序时,该用户将使用用户B所使用的身份验证提供程序升级为永久用户.

在搜索时,我遇到了-解决方案

Firebase支持针对此类情况创建匿名用户帐户.

对Firebase进行匿名身份验证

您可以使用Firebase身份验证来创建并使用临时匿名帐户对Firebase进行身份验证.这些临时的匿名帐户可用于允许尚未注册您的应用的用户使用受安全规则保护的数据.如果匿名用户决定注册您的应用,则可以链接其签名-匿名帐户中的凭据,这样他们就可以在以后的会话中继续使用受保护的数据.


已知第二个用户的电子邮件地址

如果您已经拥有尚未注册的用户(用户B)的电子邮件地址,则可以使用Firebase Admin SDK创建其帐户.

请参见> 创建用户

然后可以为使用电子邮件链接完成登录.但是,这不是问题,因为如文档示例所示,您可以提示用户输入他们的电子邮件地址.

if (firebase.auth().isSignInWithEmailLink(window.location.href)) {
  // Additional state parameters can also be passed via URL.
  // This can be used to continue the user's intended action before triggering
  // the sign-in operation.
  email = window.prompt('Please provide your email for confirmation');
  
  // The client SDK will parse the code from the link for you.
  firebase.auth().signInWithEmailLink(email, window.location.href)

  ...

现在用户B 已使用电子邮件链接成功登录,

amount: 20
fromUser: uid for User A
toUser: uid for User B

The issue here is that the User B doesn't exist yet and so there is no uid for the user B. What I want to do is to create a temporary user for User B with the email address which will generate a uid. And later when the User B signs up on the app, the same user is upgraded to a permanent user with whatever auth provider the User B has used.

While searching, I came across - https://www.freecodecamp.org/news/heres-what-i-wish-i-knew-before-i-started-using-firebase-9110d393e193/

Which mentions that this was possible with the firebase invites which is now depreciated. So, is there any other way to achieve this behavior now?

解决方案

Firebase supports creating anonymous user accounts for just such scenarios.

Authenticate with Firebase Anonymously

You can use Firebase Authentication to create and use temporary anonymous accounts to authenticate with Firebase. These temporary anonymous accounts can be used to allow users who haven't yet signed up to your app to work with data protected by security rules. If an anonymous user decides to sign up to your app, you can link their sign-in credentials to the anonymous account so that they can continue to work with their protected data in future sessions.


Email Address of 2nd User Known

If you already have the email address for a user (User B) that has not yet signed up, then you can create their account using the Firebase Admin SDK.

See Create a user

The new User B email address could then be configured for Email Link Authentication by calling firebase.auth().sendSignInLinkToEmail(email, actionCodeSettings).

Since the User B account creation was initiated by User A, you would not be able to use Local Storage to save the email of User B to complete sign-in with the email link. However, this is not a problem, since as the documentation example shows, you may prompt the user for their email address.

if (firebase.auth().isSignInWithEmailLink(window.location.href)) {
  // Additional state parameters can also be passed via URL.
  // This can be used to continue the user's intended action before triggering
  // the sign-in operation.
  email = window.prompt('Please provide your email for confirmation');
  
  // The client SDK will parse the code from the link for you.
  firebase.auth().signInWithEmailLink(email, window.location.href)

  ...

Now that User B has successfully signed in using the email link, the standard process may be followed to Link Multiple Auth Providers.

这篇关于Firebase-创建一个临时用户,直到该用户注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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