Firebase android:使用户名唯一 [英] Firebase android : make username unique

查看:328
本文介绍了Firebase android:使用户名唯一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解析将在今年年底关闭,所以我决定开始使用Firebase。我需要实现一个包含3个字段的注册流程:电子邮件,用户名和密码(电子邮件和用户名必须是我的应用程序唯一的)。因为Firebase不提供简单的方式来管理像Parse这样的用户名,所以我决定只使用电子邮件/密码注册,并保存一些额外的数据,如用户名。这里是我的用户数据结构:

  app:{
users:{
some-user- uid:{
email:test@test.com
用户名:myname
}
}
}

但是,我想要做的是使用户名唯一,并在创建帐户之前检查它。
这些是我的规则:

  {
rules:{
.read :true,
.write:true,
users:{
$ uid:{
.write:auth!== null& amp ;& auth.uid === $ uid,
.read:auth!== null&& auth.provider ==='password',
username :{.validate:!root.child('users')。child(newData.child('username').val())。exists()}
}
}


$ / code $ / pre
$ b $ p非常感谢您的帮助

解决方案

部分答案是存储一个用户名索引,您在安全规则中检查:

  app:{
users:{
some-user-uid:{
email:test@test.com
用户名:myname

},
用户名:{
myname:some-user-uid$ b $因此,用户名
$ b $ / code>

c>节点将用户名映射到一个uid。它本质上读为用户名myname由some-user-uid拥有。



有了这个数据结构,您的安全规则可以检查是否已经存在给定的用户名:

 users:{
$ uid:{
.write:auth!== null&& auth.uid === $ uid,
.read:auth!== null&&auth.provider === (),
username:{
.validate:
!root.child('usernames')。child(newData.val())。exists() ||
root.child('usernames')。child(newData.val()).val()== $ uid
}
}
}

这验证了用户名尚未被任何人声明,或者是当前用户声明的。


Parse will shut down at the end of the year, so I decided to start using Firebase. I need to implement a register process with 3 fields : email, username, password (Email & username must be unique for my app).

Since, Firebase is not providing an easy way to manage username like Parse, I decided to use only the email/password registration and save some additional data like username. Here is my users data structure :

app : {
    users: {
       "some-user-uid": {
            email: "test@test.com"
            username: "myname"
       }
    }
}

But, what I want to do is to make the username unique and to check it before creating an account. These are my rules :

{
    "rules": {
        ".read": true,
        ".write": true,
        "users": {
            "$uid": {
                ".write": "auth !== null && auth.uid === $uid",
                ".read": "auth !== null && auth.provider === 'password'",
                "username": {".validate": "!root.child('users').child(newData.child('username').val()).exists()"}
            }
        }
   }
}

Thank you very much for your help

解决方案

Part of the answer is to store an index of usernames, that you check against in your security rules:

app : {
    users: {
       "some-user-uid": {
            email: "test@test.com"
            username: "myname"
       }
    },
    usernames: {
        "myname": "some-user-uid"
    }
}

So the usernames node maps a username to a uid. It essentially reads as "username 'myname' is owned by 'some-user-uid'".

With this data structure, your security rules can check if there is already an entry for a given username:

"users": {
  "$uid": {
    ".write": "auth !== null && auth.uid === $uid",
    ".read": "auth !== null && auth.provider === 'password'",
    "username": {
      ".validate": "
        !root.child('usernames').child(newData.val()).exists() ||
        root.child('usernames').child(newData.val()).val() == $uid"
    }
  }
}

This validates that the username isn't claimed by anyone yet OR it is claimed by the current user.

这篇关于Firebase android:使用户名唯一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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