【问题标题】:Missing or insufficient permissions firestore database rules缺少或不足的权限 Firestore 数据库规则
【发布时间】:2018-08-14 01:35:01
【问题描述】:

我正在自学 Firestore,但我想不出一种只允许用户更新、删除或只读他们添加的集合的方法。

这是我正在使用的结构:

我使用 firebase auth 进行用户处理。我在每个集合的数据库中将currentUser.uid 保存为user_id

这些是我正在使用的规则

service cloud.firestore {
  match /databases/{database}/documents {

    match /tasks{
      allow read, update, delete: if request.auth.uid == resource.data.user_id;
      allow create: if request.auth.uid != null;
    }
  }

当我尝试读取/获取数据时,我收到 Missing or insufficient permissions 错误。

我将 web api (JavaScript) 用于 firestore。这是我用来读取数据的代码。

function read() {

    db.collection("tasks").get().then((querySnapshot) => {
        querySnapshot.forEach((doc) => {
            var newLI = document.createElement('li');

            newLI.appendChild(document.createTextNode(doc.data().task));

            dataList.appendChild(newLI);

        });
    });

}

【问题讨论】:

    标签: firebase data-structures google-cloud-firestore rules


    【解决方案1】:

    错误出现在我的 JavaScript 中,我在没有用户过滤的情况下得到了所有内容

    function read() {
        let taskColletion = db.collection("tasks");
    
        taskColletion.where("user_id", "==", firebase.auth().currentUser.uid).get().then((querySnapshot) => {
            querySnapshot.forEach((doc) => {
                var newLI = document.createElement('li');
    
                newLI.appendChild(document.createTextNode(doc.data().task));
    
                dataList.appendChild(newLI);
            });
            
        });
    
    }
    

    【讨论】:

    • 我也发现了。必须将安全规则过滤器添加到 where 子句。即使在您没有 where 子句的情况下(例如查询具有特定父文档 id 的分层子集合,所有子文档都知道与 where 子句匹配)
    • 详细说明您的答案正确的根本原因Rules are not Filters > 您不能为集合中的所有文档编写查询并期望 Cloud Firestore 仅返回当前客户端拥有的文档访问权限。
    【解决方案2】:

    这个其实在Firestore Documentation上都有解释(推荐阅读)。

    /tasks 后面缺少通配符:

    service cloud.firestore {
      match /databases/{database}/documents {
        match /tasks/{task} {
          allow read, update, delete: if request.auth.uid == resource.data.user_id;
          allow create: if request.auth.uid != null;
        }
      }
    }
    

    【讨论】:

    • 我已经尝试过了,但它仍然给我Missing or insufficient permissions
    猜你喜欢
    • 2018-12-06
    • 1970-01-01
    • 2019-02-26
    • 2020-10-27
    • 1970-01-01
    • 2018-04-25
    • 2019-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多