Search code examples
firebasefirebase-authentication

Firebase pending user id


Firebase has the functionality to create an auth user programmatically which generates 28 char long ID for that user. I have a case where I need to generate the same type of ID without creating a user, sort of pending user ID so that I can create a user later on with the before hand known ID.

What is the best approach to do this?

I can't seem to find an inbuilt way to generate an auth user ID without creating an auth user (one can do a similar thing for documents by calling doc().id in which case one gets 20 char-long ID)

I can surely handcraft a function that concats 28 chars using Math.random() and alphabet+numbers, but my concern is that auth user id might be more clever in helping minimize collisions (I'd like to know how it works internally if anyone is familiar with that) and I'd rather reuse what auth is using if possible rather than inventing my own wheel.

Besides, reusing the existing logic would help if anything changes internally in auth, like for example uid lengths will become 50 chars or something.


Solution

  • I have a case where I need to generate the same type of ID without creating a user, sort of pending user ID so that I can create a user later on with the beforehand known ID.

    You cannot say, hey Firebase, generate an ID for a user that will be created sometime later. So you cannot generate UIDs in advance. The UID is generated when the user is first successfully authenticated in Firebase.

    I can't seem to find an inbuilt way to generate an auth user ID without creating an auth user (one can do a similar thing for documents by calling doc().id in which case one gets 20 char char-long ID)

    You cannot find anything related to this because it doesn't exist.

    I can surely handcraft a function that concats of 28 chars using Math.random() and alphabet+numbers, but my concern is that the auth user ID might be more clever to help minimize collisions.

    If you need to reserve in advance some IDs that cannot be generated by Firebase in the way you want, then you should consider implementing your own mechanism, but always make sure to have completely unique IDs.