Loheek
Loheek

Reputation: 1985

Using provider UID as Firestore ID in AngularFire2 auth

I want to add Google, Facebook and custom login to my Angular7 web app.

Is the UID identifier given by each provider unique ? I mean, can I use it as a user ID in my users Firestore collection ?

And if that's the case, can I still use the createId() method to generate this ID myself in my custom login ? Or might it be the same as one of the providers UIDs ?

Upvotes: 0

Views: 122

Answers (1)

MichelDelpech
MichelDelpech

Reputation: 863

Yes you can use it as an UID in your users collection.

Here is my authentification flow:

1/ User creates a new account with email/password

async signUpWithPassword(email, password) {
  await firebase.auth().createUserWithEmailAndPassword(email, password)
  // user created!
}

2/ I run a cloud function with the auth's onCreate triggers to save the user to my collection:

export const createUserProfile = functions.auth
  .user()
  .onCreate((user: admin.auth.UserRecord) => {
     const { uid } = user
     const userRef = db.doc(`users/${uid}`)
     // Save it to firestore
     return userRef.set({ points: 200 })
})

3/ If this user tries to register with another provider (Google/Facebook etc.), I detect if he's using the same email address, if so I link the new provider to the current one via account linking:

https://firebase.google.com/docs/auth/web/account-linking

That way, the user is going to have multiple auth providers attached to one unique UID/profile.

Upvotes: 1

Related Questions