【问题标题】:Firebase auth database rulesFirebase 身份验证数据库规则
【发布时间】:2020-09-24 12:46:10
【问题描述】:

我正在开发需要连接到开发 Firebase 的应用程序。 这个firebase的数据库规则如下:

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

我无法理解的是如何将auth.uid 指定为确切的电子邮件地址? 据我尝试,我只得到谷歌提供的唯一 uid。 (一组数字和字母) 因此,除非我在数据库规则中指定 Google 给出的确切 uid,否则我永远无法传递身份验证以从数据库中读取,这不是一个选项,因为会有另一个用户需要访问 db 而我不需要认识他的uid

【问题讨论】:

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


    【解决方案1】:

    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"
          }
        }
      }
    }
    

    这就是你应该如何解决你的问题“会有另一个用户需要访问数据库,我不知道他的uid”。


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

    【讨论】:

      【解决方案2】:

      请尝试以下规则

      {
        "rules": {
          "users": {
            "$uid": {
              ".read": "$uid === auth.uid",
              ".write": "$uid === auth.uid"
            }
          }
        }
      }
      

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

      【讨论】:

        猜你喜欢
        • 2020-05-10
        • 2018-08-25
        • 2021-01-22
        • 2019-07-10
        • 1970-01-01
        • 2018-01-17
        • 2016-06-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多