我试图在运气好的基础上设置一些安全规则。基本上,我需要根据用户的电话号码检查用户是否被阻止的天气。这是我到目前为止的内容:

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

最佳答案

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

编辑

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

07-24 09:45
查看更多