Home > Software design >  Is it possible to randomly set a name that does not yet exist in document?
Is it possible to randomly set a name that does not yet exist in document?

Time:10-07

I'm using Firebase Firestore with Flutter.

It is common to set with user's UID in the document, but is it exist an API function standard to set this name randomly without duplication?

Is it currently common practice for the client to generate a random string, look for a document that matches its name, try again if it exists, and register its name if it doesn't exist? Anyway, if Firebase has this useful function, I don't have to do this.

CodePudding user response:

It is common to set with the user's UID in the document?

It's more likely to be used as an ID of the document. Storing it in the user document doesn't provide any benefits. Why? Because you can always get the UID from the User object, without the need to create any Firestore database calls.

but does exist API function standard to set this name randomly without duplication?

That's indeed what Firebase does. Generates a unique UID of each user. There are no duplicates.

Is it currently common practice for the client to generate a random string, look for a document that matches its name, try again if it exists, and register its name if it doesn't exist?

No, there is no need for the client to generate any random IDs since Firebase already does that. However, there is nothing built-in that can help check if a user already exists. You should explicitly do it in your app's code, and create further logic according to the result.

Anyway, if Firebase has this useful function, I don't have to do this.

It already has that. There is nothing you should do. You should only use the provided UID that comes from the authentication process.

  • Related