Firebase数据库规则的唯一用户名 [英] Firebase Database Rules for Unique Usernames

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

问题描述

我正在尝试创建一个使用Firebase数据库来存储用户信息的网站。我想要使​​用唯一的用户名。我有两个索引,一个是用户名,另一个是用户名。



我的数据库结构如下:

<$ p
$ uid {
用户名:username1,
性别:xyz
电子邮件:xyz
}
},
usernames {
username1:$ uid

用户声明用户名和他们的$ uid。



这些是我的规则:

  {
rules:{
users:{
$ uid:{
.write: auth!== null&& auth.uid === $ uid,
.read:auth!== null&& auth.uid === $ uid,
username:{
.validate:
!root.child('usernames')。child(newData.val() ).exists()||
root.child('usernames')。child(newData.val())。val()== $ uid
}
}
},
用户名:{
.write:!data.exists()&& auth!= null,
.validate:newData.val()== auth.uid< ----我不能得到这个工作
}
在$ uid下设置用户名时,它将检查用户名索引,以便用户名只能用于写一个用户名不在使用或者有它自己的$ uid。

我只希望数据中的值是认证的用户uid和关键是用户名。我不能完全得到这个工作。我怀疑我正在使用newData()。val()不正确。我的验证语句失败。

我喜欢避免使用自定义令牌,但我打开任何建议。在此先感谢。



对不起,如果这个解释太抽象了,这是我的第二篇文章

编辑#2
我做了一些研究,我可以告诉和所有我可以找到在文档中谈到需要使用.child( ).val()之前,但我需要.child采取变量,而不是一个设置的用户名。

 usernames:{
$ username:{
.write:!data.exists()&& amp ; auth!= null&& newData.val()== auth.uid

},
}


I'm trying to create a website that uses Firebase's databases to store user information. I want to use unique usernames. I have two indexes, one for users and another for usernames.

My database is structured like this:

users {
  $uid {
    username: "username1",
    gender: "xyz"
    email: "xyz"
  }
},
usernames {
    "username1": $uid"
}

The users claim a username with their $uid.

These are my rules:

{
  "rules": {
    "users": {
      "$uid": {
        ".write": "auth !== null && auth.uid === $uid",
        ".read": "auth !== null && auth.uid === $uid",
        "username": {
          ".validate": "
            !root.child('usernames').child(newData.val()).exists() ||
            root.child('usernames').child(newData.val()).val() == $uid"
        }
      }
    },
    "usernames" : {
      ".write": "!data.exists() && auth!= null",
      ".validate": "newData.val() == auth.uid"  <---- I can't get this too work
    }
  }
}

When setting username under $uid it checks the usernames index so username can only be written with a username not in use or one that has it's own $uid.

I only want data in which the value is the authenticated users uid and the key is the username. I can't quite get this to work. I suspect that I am using newData().val() incorrectly. My validate statement is failing.

I'd like to avoid using custom tokens, but I'm open to any suggestions. Thanks in advance.

Sorry if this explanation is too drawn out, this is my second post on StackOverflow.

Edit #2 I did some research from what I can tell and all I can find in docs talks about the need to use .child() before .val() but I need .child to take a variable instead of a set username.

解决方案

Sorry if i'm late but i ran into a similar problem, i changed my usernames rule to the following which did the trick:

"usernames" : {
  "$username": {
  ".write": "!data.exists() && auth!= null && newData.val() == auth.uid"
  }
 },
}

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

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