Matthew-I
Matthew-I

Reputation: 23

What uid is best to use for Firebase Firestore user document ids?

I have a firebase project for which I have created a firebase firestore database. I use firebase auth for signing users in. I want all documents in reference with a specific user to have their id equal to the users uid. At first, I set the document id to the FirebaseAuth.FirebaseUser.uid The problem with that is that the FirebaseUser.uid can change under certain circumstances (E.g. the user changes his Google account password) Is there a persistent uid I can use to refer to a specific user no matter the authentication provider ? If not, what would be best practice ?

Upvotes: 1

Views: 696

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83068

Normally, for a given user in Firebase Auth, the user unique ID (the uid generated by the Auth service) will never change.

For example, you can, with the Admin SDK, change the email of a user without changing its uid, see this doc.

You can also have several identity providers for the same account, with the same uid, see here:

You can sign in users to your apps using several methods: email address and password, federated identity providers, and your custom auth system. You can associate more than one sign-in method with a user: for example, a user can sign in to the same account using an email address and a password, or using Google Sign-In

Note that changing the password of an identity provider account (e.g. "changing the Google account password" as you mentioned) has no impact at all on the uid or on the other user account elements (actually the identity provider password is not stored in Firebase Auth).


So, in conclusion, using the uid generated by the Auth service is the best solution for your requirement.

Upvotes: 3

Related Questions