【问题标题】:How to read collection only if UID matches collection name in Firebase Firestore?仅当 UID 与 Firebase Firestore 中的集合名称匹配时,如何读取集合?
【发布时间】:2019-05-05 12:15:40
【问题描述】:

我为每个用户创建了一个用户 ID 集合,并且我想设置数据库规则以仅当用户 ID 与集合项名称匹配时才读取集合项。 我试过类似的东西:

 service cloud.firestore {
    match /databases/{database}/documents {
      match /{document=**} {
        allow read: if request.auth != null;
      }
      match /{userId} {
        allow read: if belongsTo(userId);
      }
      function belongsTo(userId) {
        return request.auth.uid == userId
      }
    }
    }

【问题讨论】:

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


    【解决方案1】:

    Firestore 读取文档。没有阅读收藏的概念。因此,您的规则必须提供对文档的访问权限。

    授予对以用户 UID 命名的集合中所有文档的访问权限:

      match /{userId}/{document} {
        allow read: if belongsTo(userId);
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-08
      • 1970-01-01
      • 2021-11-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多