Firebase 身份验证规则检查电子邮件

Firebase authentication rule checks email

我想只允许具有已在用户列表中的电子邮件地址的经过身份验证的用户写入权限。

我的用户列表如下所示:

{
  "users" : {
    "-KeZg-MuD-4TEOiW9i0_" : {
      "email" : "example@gmail.com"
    }
  }
}

我试过使用这样的规则:

"users": {
  ".write" : "root.child('users/email').val() === auth.token.email"
}
"users": {
  ".write" : "root.child('users.email').val() === auth.token.email"
}
"users": {
  ".write" : "root.child('users.email').child(auth.token.email).exists()"
}
"users": {
  ".write" : "root.child('users').child(auth.token.email).exists()"
}

但无济于事。当我尝试像这样添加新用户时,我仍然收到权限被拒绝的错误:

firebase.database().ref('users').push({email: 'example@gmail.com'})

我上面的代码片段使用 example@gmail.com 而不是实际的 google 经过身份验证的用户的电子邮件地址,但实际用户存在于我的用户数据库列表中。

在您分享的 JSON 中没有路径 /users/email。所以这个规则永远不会是 true:

root.child('users/email').val() === auth.token.email

您不能在安全规则中搜索特定 的路径。不过,您可以检查特定 key 是否存在。参见 , or my answer in this #AskFirebase video: https://youtu.be/66lDSYtyils?t=6m15s