【问题标题】:Firestore security rules issues with phone number电话号码的 Firestore 安全规则问题
【发布时间】:2019-02-06 07:24:32
【问题描述】:

我正在尝试在 Firebase 中设置一些安全规则,但没有运气。基本上我需要根据用户的电话号码检查用户是否被阻止的天气。这是我目前所拥有的:

service cloud.firestore {

  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if exists(/databases/$(database)/documents/access/+17777777777);
    }

    match /globals/{document=**} {
      allow read: if true;
    }

    match /requests/{document=**} {
      allow write: if true;
    }
  }
}

如果我在规则本身中对数字进行硬编码,它会执行应有的操作。如果我使用$(reqest.auth.token.phone_number) 它不起作用。

allow read, write: if !exists(/databases/$(database)/documents/access/$(reqest.auth.token.phone_number));

我也按照this question 尝试过get

allow read, write: if get(/databases/$(database)/documents/access/$(reqest.auth.token.phone_number)).blocked == true ||
      get(/databases/$(database)/documents/access/$(reqest.auth.token.phone_number)).data.blocked == true;

我的数据结构是这样的

access | +17777777777 | blocked = true

我也尝试过翻转结构:

access | blocked | +17777777777 = true

这是来自模拟器的身份验证负载

{
  "uid": "19687a6s87d68as7d968as7d9a8sd",
  "token": {
    "sub": "19687a6s87d68as7d968as7d9a8sd",
    "aud": "my-app",
    "email": "",
    "email_verified": false,
    "phone_number": "+17777777777",
    "name": "",
    "firebase": {
      "sign_in_provider": "google.com"
    }
  }
}

【问题讨论】:

    标签: firebase google-cloud-firestore firebase-security


    【解决方案1】:

    问题不在于$(reqest.auth.token.phone_number) 方法。正如this answer 中所述,不幸的是,Firestore 目前不支持文档路径中的引用字段值,这就是为什么只有硬编码值起作用的原因。

    编辑

    这很奇怪,但Firestore's example 仍然与我上面的回答相矛盾。

    【讨论】:

    • 感谢您的回答。我很困惑。我最终使用了 UID。他们以某种方式工作¯\_(ツ)_/¯
    【解决方案2】:

    我找到了解决这个问题的方法!根据documentation,也可以使用 path() 函数构造路径。因此,您可以使用字符串连接手动构建路径:

    allow read, write: if exists(path("/databases/" + database + "/documents/access/" + request.auth.token.phone_number));
    

    【讨论】:

      【解决方案3】:

      如果有帮助,当您的身份验证是电话身份验证时,您的 $uid 将是国际格式的电话号码。

      {
        "rules": {
          "users": {
            "$uid" : {
            ".read": "auth != null",
            ".write": "auth != null &&  $uid === auth.token.phone_number"
             }
            }
         }
      }
      

      【讨论】:

        猜你喜欢
        • 2019-08-14
        • 1970-01-01
        • 2021-01-16
        • 2019-08-20
        • 1970-01-01
        • 2018-10-19
        • 2019-07-05
        • 1970-01-01
        • 2020-06-19
        相关资源
        最近更新 更多