简体   繁体   English

是否可以缩短 firebase 身份验证 UID?

[英]Is it possible to shorten firebase auth UIDs?

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.我已经开始设计一个推荐系统,并希望使用 firebase 身份验证 UID 作为每个用户的推荐代码,但是它们比我想要的要长,也不能保证保持小。 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.我最近在 npm 上遇到了可爱的短 uuid pkg,但不幸的是它似乎不适用于 firebase UID(因为那些不是 UUID)但我一直在寻找不涉及保留的可能解决方案自定义 ID 到 UID 的查找表。

So the real question: is there any good way to compress a short string programmatically and then decompress?所以真正的问题是:有没有什么好的方法可以以编程方式压缩一个短字符串然后解压缩?

There is no way for you to control the UIDs that Firebase Authentication generates in its various providers.您无法控制 Firebase 身份验证在其各种提供程序中生成的 UID。

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.您可以自己生成一个较短的 ID,并维护这些 ID 到 Firebase 生成的 ID 的映射。 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.您通常希望对较短的 ID 进行唯一性检查,因为较短的字符串会迅速增加 go 的碰撞机会。
  • 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.这本质上是第一个选项的变体,但现在具有用户选择的 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.您还可以创建插入 Firebase 身份验证的自定义提供程序 When doing this, you control the UID that Firebase uses too.执行此操作时,您也可以控制 Firebase 使用的 UID。 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.在这里,您有责任确保您的提供者生成的 UID 不仅在您自己的提供者中是唯一的,而且在您为项目启用的所有提供者中都是唯一的。

As @FrankVanPuffelen explained there is no way for you to control the UIDs that Firebase Authentication automatically generates.正如@FrankVanPuffelen 解释的那样,您无法控制 Firebase 身份验证自动生成的 UID。

But with the createUser() method of the Admin SDK you can define which UID you want to assign to a new user.但是使用Admin SDKcreateUser()方法,您可以定义要分配给新用户的 UID。 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"如文档中所述,“如果您想为新用户指定自己的 UID,则可以将其作为参数包含在传递给用户创建方法的参数中”

await admin.auth().createUser(
  {
    uid: 'some-uid',
    email: 'user@example.com',
    password: '....',
  }
);

You can run this code in a Cloud Function or on a server you own.您可以在Cloud Function或您拥有的服务器上运行此代码。

Of course, you need to ensure that the user's UID is unique for each user.当然,您需要确保用户的 UID 对于每个用户都是唯一的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM