【问题标题】:Firebase rules - read only value which was in queryFirebase 规则 - 查询中的只读值
【发布时间】:2019-11-10 18:14:42
【问题描述】:

我将用户名单独存储在数据库的验证部分中。只有用户名,这样用户在注册的时候,可以在注册前输入用户名,看看用户名是否可用。 现在的规则是 ".read": true ,因此基本上允许任何人轻松访问数据库中的所有用户名。我想阻止这种情况,但我不知道该怎么做,因为我不能使用 auth != null 规则,因为尚未创建用户。

我唯一的想法是只允许读取请求的用户名,即某些东西

 firebase.database().ref('Validations/Usernames')
            .orderByChild('name')
            .equalTo(username)
            .once('value')
            .then(snapshot => ....

然后使用这样的规则:

"$name": {
   ".read": "query.equalTo == $name"
}

即允许从客户端检查的用户名只读。现在,这失败了,因为对于所有其他人,它与 orderByChild 的检查它无权访问这些。

是否有一些类似的方法可以只读取请求从客户端读取的值?或者,在他创建帐户之前,我应该以其他方式验证用户名?

【问题讨论】:

    标签: javascript firebase firebase-realtime-database firebase-security


    【解决方案1】:

    如果您只想让用户检查他们输入的用户名是否已被声明,他们不会读取整个Validations/Usernames 节点的访问权限。相反,他们只需要对特定子项的读取权限。

    为此,请确保使用用户名作为Validations/Usernames 下的键,例如:

    "Validations": {
      "Usernames": {
        "CosmicSeizure": "uidOfCosmicSeizure",
        "puf": "uidOfPuf"
      }
    }
    

    有了上面你的规则可以是:

    "Validations": {
      "Usernames": {
        "$name": {
          ".read": true
        }
      }
    }
    

    然后您可以检查用户名是否存在:

    firebase.database().ref('Validations/Usernames')
            .child(username)
            .once('value')
            .then(snapshot => ....
    

    然后你检查snapshot.exists()

    【讨论】:

      猜你喜欢
      • 2020-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多