【问题标题】:Firebase firestore security rules for public and private collections适用于公共和私人收藏的 Firebase Firestore 安全规则
【发布时间】:2018-10-04 08:06:01
【问题描述】:

我有 2 个集合 subscribersposts。我已将安全规则设置为read: write when authenticated。但是我需要subscribers 集合来写入数据而无需身份验证,并在posts 集合中检查身份验证,然后编写如何实现这一点?

【问题讨论】:

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


    【解决方案1】:

    设置 firestore 规则,您也可以在 firebase 模拟器中对其进行测试。

    service cloud.firestore {
      match /databases/{database}/documents {
        match /subscribers/{document=**} {
          allow read, write : if true;
        }
        match /posts/{document=**} {
          allow read : if true;
          allow write: if request.auth.uid != null;
        }
      }
    }
    

    答案非常具体到您想要在为posts 集合进行身份验证时写入数据的问题。我考虑过,读取数据还是对所有人开放的。

    【讨论】:

    • 工作.. 但假设我有多个收藏,我只需要 subscribers 成为 write: if true。我是否必须写 `match /posts/{document=**} { 允许阅读:如果为真;允许写入:如果 request.auth.uid != null; }` 这对所有其他集合?
    • 如果你有多个数据库,你必须为每个数据库编写单独的规则。但是,如果您有多个集合,如果您不希望将任何数据库规则应用于其他集合,则无需为其他集合编写任何内容。
    【解决方案2】:

    应该这样做 -

    service cloud.firestore {
      match /databases/{database}/documents {
        match /subscribers/{document=**} {
          allow read, write;
        }
        match /posts/{document=**} {
          allow read, write: if request.auth.uid != null;
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-03-17
      • 2013-11-22
      • 1970-01-01
      • 2021-04-09
      • 1970-01-01
      • 2017-06-24
      • 2016-12-19
      • 2020-07-03
      • 2020-06-03
      相关资源
      最近更新 更多