Firebase身份验证数据库规则

时间:2020-06-05 10:24:28

标签: firebase firebase-realtime-database firebase-authentication google-authentication

我正在开发需要连接到开发者Firebase的应用程序。 此Firebase具有以下数据库规则:

"rules": {
    // no read access at root level
    ".read": "auth.uid === 'emailaddressgmailcom'",
    ".write": false,  

我不明白如何将auth.uid指定为确切的电子邮件地址? 据我尝试,我只能得到Google提供的唯一uid。 (一组数字和字母) 因此,除非我在数据库规则中指定了Google给出的确切uid,否则我永远都不能通过auth来从数据库中读取数据,这是不可行的,因为我将有另一个用户需要访问db不知道他的uid

2 个答案:

答案 0 :(得分:0)

authpredefined variables之一。

通过执行auth.uid,您将获得用户ID(“保证在所有提供商中都是唯一的”)。

您需要在安全规则中使用它来定义给定用户对一个或多个给定资源的访问权限,如文档中的here所述。

如果特定资源应由唯一用户读取,则可以将其与固定值进行比较:

".read": "auth.uid === 'HGH656675FHGFGHF3454'"

,但是通常您将其与要保护的节点/资源路径的某些部分进行比较,例如:

{
  "rules": {
    "users": {
      "$user_id": {
        // grants write access to the owner of this user account
        // whose uid must exactly match the key ($user_id)
        ".write": "$user_id === auth.uid"
      }
    }
  }
}

这是解决“存在另一个需要访问db的用户,但我不知道他的uid的用户”问题的方法。


我建议您阅读有关RTDB安全规则的整个部分,以了解更多详细信息:https://firebase.google.com/docs/database/security

答案 1 :(得分:0)

请尝试以下规则

scale_y_continuous

//这些规则授予对已验证身份的节点的访问权限 //来自Firebase身份验证令牌的用户ID