I am working on a project, where I want the users to able to view each others profiles.
I want the URL to be like GitHub, so my-project.com/username
. To achieve that I want to set a custom uid for every user. I don’t want the uid to be random characters like it is by default in Firebase authentication. I want it to be a unique and readable username.
I tried researching inside Firebase docs about this, but I could not find anything. the closest thing to the way the auth cloud function trigger functions.auth.user().beforeCreate(). But this is not what I need because through this I cannot set the uid.
2
Answers
If you don’t want to use the UID, which is a unique identifier for your users, you can indeed use a username. Since the two users can choose the same user name, before letting a user create a new account, you always have to check the chosen user name for existence. So it’s a two-step operation.
I don’t know what kind of authentication are you using in your project, but if you want to have the chosen user name available across your entire project, don’t forget to update the
displayName
field inside theFirebaseUser
object.If you don’t want to perform such an operation, then you can indeed use functions.auth.user().beforeCreate() which:
You can use the
createUser()
method of the Admin SDK to create a user in the Auth service with specifying your own uid, as explained in the documentation:You can execute this Admin SDK code in a Cloud Function (recommended option) or from a server that you control. In other words this method cannot be called from a client (i.e. front end). However you could call, from a Client, a Callable Cloud Function which uses this method: see this article for an example.
You are responsible to choose the custom uid value and to ensure it’s unicity. It must be "a string between 1-128 characters long, inclusive" and must be unique (if a user with a same id exists, the
createUser()
method will return an error).