确保在 Firebase (Swift) 中创建新用户时具有唯一的用户名 [英] Ensuring unique username on creating a new user in Firebase (Swift)

查看:13
本文介绍了确保在 Firebase (Swift) 中创建新用户时具有唯一的用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Firebase 的新手,所以任何提示都将不胜感激.现在我正在使用 Firebase Login &Auth 为我处理所有身份验证.但是我想存储一些用户数据,例如用户名 DOB、created_at... 我已经创建了一个用户节点,并且我正在使用我在注册或登录时获得的唯一 ID 将子节点附加到用户.但是我想确保系统中没有重复的用户名.如何在写入服务器之前检查用户名是否已存在?

I am a newbie to Firebase so any hint will be appreciated. Right now I am using Firebase Login & Auth to handle all the authentication for me. But I want to store some user data, like username DOB, created_at... I have created a users node, and I am appending children nodes to users using the unique id I get on sign up or login. However I want to ensure that there are no duplicate usernames in the system. How can I check if a username already exists before writing to the server?

推荐答案

我目前正在调查同样的事情.我的问题是我还希望启用持久性,因此我需要了解可以离线访问的内容.如果您使用持久性,我还建议您禁止特定操作,例如在您的客户端离线时检查用户名是否存在,您可以通过收听.info/connected"来执行此操作,详情如下:

I'm currently investigating the same thing. My issue is that I also want persistence enabled, so I need to be aware of what can be accessed offline. If you're using persistence, I would also recommend disallowing particular operations such as checking username existence if your client is offline, which you can do by listening to ".info/connected" as further detailed here:

https://firebase.google.com/docs/database/ios/offline-capabilities#section-connection-state

我个人的工作流程如下:

My personal workflow for this is as follows:

  1. 登录用户帐户
  2. 检查用户是否已有用户名

检查 firebase 数据库,看看他们的用户详细信息是否包含用户名:

Check the firebase database to see if their user details includes a username:

DB/users/*userUID*/username != nil

  1. 如果他们没有用户名,则提示他们设置用户名.

当他们设置用户名时,检查用户名是否存在于:

When they set their username, check if the username exists in:

DB/usernames/*username*/ != nil

如果不存在,则将 username 和 userId 写入上面检查的两个数据库位置.

If it doesn't exist, then write the username and userId in the two database locations checked above.

例如

user.uid = wewe32323

user.uid = wewe32323

用户名 = scuba_steve

username = scuba_steve

DB/usernames/scuba_steve = wewe32323
DB/users/wewe32323/username = scuba_steve

所以现在您有了 DB/usernames 参考,您可以快速检查是否有人已经拥有用户名,并且您还有 DB/users/可以在其中快速找到给定用户的用户名.

So now you have the DB/usernames reference that you can check quickly to see if anyone has a username already, and you also have DB/users/ where you can quickly find a username for a given user.

我不会说这是万无一失的,因为我仍然对并发请求有一些担忧.我目前正在调查的问题是,假设您删除了与特定用户的用户名关联,该用户可以依赖其本地数据库副本错误地断言他们仍分配给该用户名.

I won't say this is fool-proof, as I still a few concerns around concurrent requests. My current issue I'm investigating is that lets say you delete the username association to a particular user, the user can depend on their local copy of the database to incorrectly assert that they are still assigned to that username.

您可以查看数据库写入规则以禁止任何人修改现有数据(强制您只能在没有现有数据的情况下写入 DB/usernames 目录.这将防止覆盖首先设置用户名的人,从而我认为这是重要的一步.

You could look into the database write rules to disallow anyone to modify existing data (enforcing that you can only write to the DB/usernames directory if there is no existing data. This would prevent overriding of whoever sets the username first, which I think is an important step.

可能也值得研究交易:

https://firebase.google.com/docs/database/ios/save-data#save_data_as_transactions

但我相信正确的写作规则应该允许可靠的写作.

But I believe correct write rules as mentioned in the paragraph above should allow dependable writing.

这篇关于确保在 Firebase (Swift) 中创建新用户时具有唯一的用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆