AGoranov
AGoranov

Reputation: 2244

Custom UID for each user created for Firestore

Please note that this question is same as the question here. I am not satisfied with the answer and that's the reason why I am asking it anew.

My problem is that I have a lot of data stored in an external API, each with unique ID. I have imported this data into a separate document (let's say users) into firestore from where I will access it into custom application. Additionally, I have authentication. I want for each user in users document to be created with a specific uid and not the one automatically generated from Firebase.

Is this even possible? If not, then what can I do?

Upvotes: 0

Views: 1180

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598740

The UID of a Firebase Authentication user is determined by the provider that initially created that user.

This means that the only way to control the UID is to create a custom provider. Note that this requires that you run code that creates custom tokens for users in a trusted environment, such as your development machine, a server that you control, or Cloud Functions. In that environment, you can then call the createCustomToken API, where you pass in the UID of your choice. For an example of this see this custom username/password provider that uses the username as its UID.

If creating a custom provider is not something you want to do, your only alternative is to maintain a mapping between the ID that you have, and the UID that Firebase generates. So for example, you'd store a document with the Firebase UID, and then in there have a field with your own ID for that user, so that you can look up the information in both directions.

Upvotes: 2

Related Questions