0

When one creates a user in Firebase Auth, Firebase Auth generates a userId using some internal algorithm.

Is their a way to use the same logic/function to generate userId but without actually creating a user? If yes, how?

vir us
  • 9,920
  • 6
  • 57
  • 66
  • Yes, you can certainly generate some random alphanumeric string on your own. What specifically are you stuck on with that task? Please edit the question to be clear. Right now, your question can be answered with a simple yes or no, [which is probably not what you would find helpful](https://overflow.tips/problematic-questions#yes-no-question). – Doug Stevenson Jul 16 '23 at 12:52
  • ok @DougStevenson - how can I use the same firestore function to generate userId as firestore uses when user is created? – vir us Jul 16 '23 at 13:02
  • I'm not clear what you mean by "the same firestore function". What is your concern here? Is there some problem you're trying to solve? BTW I didn't downvote. – Doug Stevenson Jul 16 '23 at 13:04
  • I'll point out that Firestore doesn't have a concept of a user. It just stores data in documents and collections. Firebase Authentication (a different product entirely) keeps track of users by way of a user ID. Are you asking about that? What is it that you previously tried that doesn't work the way you want? – Doug Stevenson Jul 16 '23 at 13:13
  • @DougStevenson I mean when firebase creates a user, it internally generates userId and it uses some logic/function for that (initially I thought creating a docRef uses the same logic for docIds). I'm trying to figure out if firestore sdk (admin or client-side) provides a way to access that function so I can generate userId using the same logic as firestore. While I can surely create my custom function, my concern is to at least ensure ids don't clash. And I'd rather rely on the same logic that firebase uses if possible. – vir us Jul 16 '23 at 13:21
  • As a workaround, I can probably create a user, read the id, then delete the user right away and later on when the check is passed, use the same id to again create a user. That would work, but looks clunky to say the least. – vir us Jul 16 '23 at 13:21
  • @DougStevenson Yes, I'm talking about Firebase Authentication and user IDs that are generated there - can I generate such an id programmatically using the same logic that Firebase Authentication uses? Or that function/logic is not exposed? – vir us Jul 16 '23 at 13:25

1 Answers1

2

It's not entirely clear what your concern is, but there are a few facts to know that might help you move forward.

  • When Firebase Authentication creates a new user, the ID it generates for that user is just a random string of a certain length. There is no meaning to that string at all - it is just unique.

  • When Firestore creates a new document with a random ID, it is also just a random string. There is no meaning to that string at all - it is just unique.

  • The random IDs above have nothing to do with each other. It is merely conventional for developers to use the Auth user ID as a document ID because it makes user data easy for code to find and protect those documents with security rules.

If you want to generate an ID for a user before it's actually created in Firebase Authentication, you are free to do so, but you cannot use the mobile or web client SDK to create the user with that ID. You will have to use the Firebase Admin SDK on your backend to create the user.

Again, it doesn't really matter how you create any of these IDs, as long as they are unique (the longer the random string, the less likely there is of a collision) and they contain only valid characters (alphanumerics). There is no logic involved that you have to follow. Firebase just provides default behavior as a convenience.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Thank you, that's all clear. I guess I just don't trust my custom implementation to generate userId and I was trying to figure out if I can reuse something that firebase already using for that. I guess the answer to that is no? I was trying to have a look at the source code of BaseAuth.prototype.createUser to see if I can find how userId generating logic works, but no luck. – vir us Jul 16 '23 at 13:44
  • Why don't do you do a search for "generate a random alphanumeric string" in whatever language you're working with? There are plenty of things to copy from right here on SO. – Doug Stevenson Jul 16 '23 at 14:27
  • Because I don't want to reinvent wheel and was just trying to see if I can reuse tested and working algorithm that fireabse is using already. Seems like the answer is "no", which is fine. Thanks for your help, appreciate it a lot. – vir us Jul 16 '23 at 19:09