Home > Net >  Create identical user in firestore based on firebase authentication
Create identical user in firestore based on firebase authentication

Time:05-31

so I am currently working on a project where I have implemented Authentication with Firebase and Google as the only sign-in/up provider. The authentication process works fine, however I want to add more and highly specific properties to an authenticated user and be able to manipulate those. For this I am planning to use Google Firestore.

The problem now is, how can I mirror a created user account from firebase auth, to firestore. I thought about just writing it to firestore based on the UID whenever the signIn function is called, however than there is the problem that there will be more than one entry for the same user I reckon.

I have read up on some blog, that the desired outcome is possible to be achieved by using cloud functions, however I am looking for a solution that I can implement in my next project, even though I might switch to cloud functions later.

Do you have an idea how I can achieve the desired outcome? Thank you for your help, it is much appreciated!

CodePudding user response:

The approach you describe in your answer (i.e. "mirror a created user account from Firebase auth to Firestore") is indeed a standard one.

You mention the following risk:

There will be more than one entry for the same user I reckon

If the question is "How to deal with the risk of having different users with the same user's Doc ID" if you use the userID (uid) generated by the Authentication service as the doc ID you can be sure it is unique.

If the question is "How can I create several user's docs for one user, containing different info" you can very well use different collections and, again, use the userID (uid) generated by the Authentication service as the documents ID (each doc being in a different collection).


You basically have two options to create the Firestore user's document:

Via a Cloud Function: In this case you would trigger the Cloud Function on user's creation and use the user's uid to create the Firestore document.

 exports.createUserDoc = functions.auth.user().onCreate((user) => {
  const userId = user.uid;
  return admin.firestore().collection('users').doc(userId).set( {...} );
});

From the front-end: You'll need to set a security rule that allows a user to create his own Firestore user's document only if the doc ID is equal to his userId. Here is an example:

service cloud.firestore {
  match /databases/{database}/documents {
    // Make sure the uid of the requesting user matches the ID of the user
    // document. The wildcard expression {userId} makes the userId variable
    // available in rules.
    match /users/{userId} {
      allow create: if request.auth != null && request.auth.uid == userId;
    }
  }
}

CodePudding user response:

There's no need to, because both services would assume the same user identity = UID.
Cloud Functions offers Firebase Authentication Triggers, which provide the event.uid.
Based on event & UID, you can use Firebase/Firestore admin, to add or remove a node.

  • Related