Home > Net >  Is it possible to shorten firebase auth UIDs?
Is it possible to shorten firebase auth UIDs?

Time:01-18

I've started designing a referral system and wanted to use firebase auth UIDs as the referral code for each user, however they're longer than I'd like and also not guaranteed to stay small. I've looked into some libraries for short text compression, hashing, etc; But none seem to satisfy my needs. I've recently came across the lovely short-uuid pkg on npm but unfortunately it doesn't seem it works with firebase UIDs(because those aren't UUIDs) but i've been looking for a possible solution that doesn't involve keeping a lookup table of custom IDs to UIDs.

So the real question: is there any good way to compress a short string programmatically and then decompress?

CodePudding user response:

There is no way for you to control the UIDs that Firebase Authentication generates in its various providers.

If you want to use a shorter/friendlier scheme for identifying your users, some options are:

  • You can generate a shorter ID yourself, and maintain a mapping of those IDs to the ones that Firebase generates. You'll typically want to include a uniqueness check for your shorter IDs, as the chances of collisions rapidly go up for the shorter strings.
  • A common example of a friendlier identifier for users is to allow users to pick a unique username. This is essentially a variant of the first option, but now with a user-selected ID. Here too you will need to perform a check to prevent duplicates.
  • You can also creating a custom provider that plugs into Firebase Authentication. When doing this, you control the UID that Firebase uses too. Here you are responsible that the UIDs your provider generates are unique not just within your own provider, but across all providers you've enabled for your project.

CodePudding user response:

As @FrankVanPuffelen explained there is no way for you to control the UIDs that Firebase Authentication automatically generates.

But with the createUser() method of the Admin SDK you can define which UID you want to assign to a new user. As explained in the doc, "if you instead want to specify your own UID for the new user, you can include it as an argument passed to the user creation method"

await admin.auth().createUser(
  {
    uid: 'some-uid',
    email: '[email protected]',
    password: '....',
  }
);

You can run this code in a Cloud Function or on a server you own.

Of course, you need to ensure that the user's UID is unique for each user.

  •  Tags:  
  • Related