Firebase - create a temporary user until the user signs up

不羁岁月 提交于 2019-12-24 04:57:15

问题


I have a use case where User A can say that User B borrowed from User A some amount of money, similar to apps like Splitwise.

I'm using firestore to store the data. In this particular case, I'll store it as a document in the "Transactions" collection which will have the following fields:

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?


回答1:


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.




回答2:


The server side (aka Node.js) Admin SDK allows you to create users programatically. You would need some way to pin/stick the newly created user UID to userB. Email address would seem the easiest way. So UserA would need to specify the email address of userB, then you process that server side Admin SDK.

When user B signs in with their email address, Firebase Authentication will detect an existing account with userB's email address and throw an error which you can use to merge userB's account data.



来源:https://stackoverflow.com/questions/57663477/firebase-create-a-temporary-user-until-the-user-signs-up

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