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

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

问题描述

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

解决方案

我目前正在调查同样的事情。我的问题是,我也希望启用持久性,所以我需要知道什么可以离线访问。如果您使用的是持久性,我还建议您不要执行特定的操作,例如,如果您的客户端处于脱机状态,请检查用户名是否存在,您可以通过聆听.info / connected进行更详细的操作:



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



我的个人工作流程如下:
$ b


  1. 登入使用者账户

  2. 检查使用者是否已有使用者名称


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

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




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

    他们的用户名,检查你是否sername存在于:

      DB /用户名/ *用户名* /!=无


    如果它不存在,那么将用户名和userId写入上面检查的两个数据库位置。



    例如

    user.uid = wewe32323

    用户名= scuba_steve

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

    所以现在你有DB /用户名引用,你可以快速查看是否有人有用户名,而且你也有DB / users /你在哪里可以快速找到一个给定用户的用户名。



    我不会说这是傻瓜证明,因为我还有一些关于并发请求的问题。我正在调查的当前问题是让我们说你删除用户名关联到一个特定的用户,用户可以依靠他们的本地数据库副本错误地断言,他们仍然被分配到该用户名。



    您可以查看数据库写入规则,以禁止任何人修改现有数据(强制只有在没有现有数据的情况下才能写入数据库/用户名目录),这样可以避免重写设置用户名首先,我认为这是一个重要的步骤。

    这也许是值得调查交易:

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

    但是我相信上面提到的正确的写入规则应该允许可靠的写入。


    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?

    解决方案

    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. Login to user's account
    2. Check if the user already has a username

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

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

    1. If they don't have a username, then prompt them to set a username.

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

    DB/usernames/*username*/ != nil
    

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

    eg.

    user.uid = wewe32323

    username = scuba_steve

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

    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.

    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.

    It may also be worth investigating Transactions:

    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天全站免登陆