Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase - create a temporary user until the user signs up

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?

like image 650
Akshay Jain Avatar asked Aug 26 '19 19:08

Akshay Jain


1 Answers

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.

like image 103
Christopher Peisert Avatar answered Oct 21 '22 03:10

Christopher Peisert