繁体   English   中英

在安全规则中验证 firebase 数据库的密钥中的 email 地址

[英]Validate email address in key of firebase database in security rules

我有一个配置错误的数据库安全规则的工作项目。

这是我目前的规则 -

{
  "rules": {
    "$email": {
         ".read": "$email == auth.token.email.replace('.',',')",
         ".write": "$email == auth.token.email.replace('.',',')",
       },
    "test": {
      ".read": "auth != null",
      ".write": "auth != null"
    }
  } 
}

这是我的数据结构 -

在此处输入图像描述

我使用 email 作为密钥,我试图将用户限制为他们自己的 email 密钥。 所以我在安全规则中使用了 email。

我正在尝试替换“。” 在我的安全规则中使用“,”作为“。” 是禁止字符。

什么是正确的语法?

我收到权限被拒绝错误。

编辑:-似乎我可以将数据写入数据库就好了。 这是导致问题的其他原因。

编辑 2:- 是写权限给了我权限被拒绝错误,但无论如何都将数据写入数据库。 我将写权限设置为 true,一切正常。 但显然我不想那样。

我可以读取我自己的节点而不会出现您的安全规则问题,并且在读取其他人的节点时会被拒绝。

我的代码:

firebase.auth().signInWithEmailAndPassword("i@puf.io", "correcthorsebatterystaple")
.then(function() {
  ref.child("i@puf,io").once("value").then(function(snapshot) {
    console.log("Got value from my own node: "+snapshot.val());
  }).catch(function(error) {
    console.error("Error while reading my own node: "+error);
  });
  ref.child("someoneelse").once("value").then(function(snapshot) {
    console.log("Got value from other node: "+snapshot.val());
  }).catch(function(error) {
    console.error("Error while reading other node: "+error);
  });
});

我的规则

"66872665": {
  "$email": {
    ".read": "$email == auth.token.email.replace('.',',')"
  }
},

还有我在数据库中的 JSON:

"66872665": {
  "i@puf,io": "value"
}

运行代码给了我这个 output:

从我自己的节点获得价值:价值

读取其他节点时出错:错误:permission_denied at /66872665/someoneelse:客户端无权访问所需数据。

有关您可以运行的工作重现,请参阅: https://jsbin.com/jomipef/edit?js,console

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM