FiRestore没有执行操作的权限 [英] Firestore no permissions to execute operation

查看:15
本文介绍了FiRestore没有执行操作的权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在FiRestore中建立规则,其中每个人都可以读取彼此的内容,但只有文档的所有者可以创建、写入、更新或删除这些内容。

我在FiRestore中设置了以下规则:

rules_version = '2';

service cloud.firestore {
  match /databases/{database} {
    match /codes/{userID} {
      allow create, write, update, delete: if request.auth.uid == userID;
      allow read: if request.auth.uid != null;
    }
  }
}

我的FiRestore结构是:

Collection 'codes' > Document 'BNhBibYZ0ThCNCH2gzPRufFsIk22' > nothing in here

我的要求是:

firestore()
  .collection('codes')
  .doc(this.state.userID)
  .onSnapshot((querySnapshot) => {
    if (!querySnapshot) {
      let testObj = {"hello": "world"} 
      firestore().collection('codes').doc(this.state.userID).set(testObj);
    }
  });

我收到的输出是:Error: [firestore/permission-denied] The caller does not have permission to execute the specified operation.

我做错了什么?

推荐答案

您可以使用我创建的以下函数来执行此操作

function isUserAuthenticated() {
    return request.auth.uid != null; 
}

function belongsTo(userId) {
    return request.auth.uid == userId;
}

然后您可以这样使用它:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if false;
    }

    match /codes/{userId} {
      allow read : if isUserAuthenticated();
      allow create : if belongsTo(userId);
      allow update: if belongsTo(userId);
      allow delete: if belongsTo(userId);
    }

    /* Functions */
    function isUserAuthenticated() {
      return request.auth.uid != null; 
    }

    function belongsTo(userId) {
      return request.auth.uid == userId;
    }
  }
}   
如果对您有效,请接受作为答案。可在评论中查看更多问题。

这篇关于FiRestore没有执行操作的权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆