I have a signup activity where users sign into firebase. But I want each user to have a different username of the format username#4-digit-code
. How can I achieve this with Firestore as i found out with this post that we can't use queries in our transactions?
What would be the best approach for it to prevent two users from having the same userid?
CodePudding user response:
What would be the best approach for it to prevent two users from having the same userid?
The best option that you have is to check if the username#4-digit-code
already exists in Firestore. If it doesn't, add it with the corresponding data, otherwise, generate a new one. This operation should continue until you find an user name which is available.
If you want to make sure you aren't doing many checking operations, then you have to make sure you always generate unique user names, or you can use Firebase Authentication, and instead of that username#4-digit-code
you can use the UID that comes from the authentication process.