无法在FiRestore规则中将quest.auth.id与文档ID匹配 [英] Unable to match request.auth.id with document id in Firestore rules

查看:15
本文介绍了无法在FiRestore规则中将quest.auth.id与文档ID匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一款带有Ffltter的应用程序。我在FiRestore上有一个数据库,结构为:USERS/(USERID)/COLLECTION/DOCUMENT 其中,userid是创建(Userid)文档的用户的唯一uid。 用于获取用户的uid的代码是

user.uid;

其中User是Firebase User的实例。 我正在尝试设置规则,以便只有当quest.auth.uid与(用户ID)文档ID匹配时,才只有用户可以读/写。

service cloud.firestore {
  match /databases/{database}/documents {
    match /Users/{documentID} {
      allow read, write: if isOwner(documentID);
    }

    function isOwner(documentID){
        return request.auth.uid == documentID;
    }
  }
}

但我在这方面遇到错误

失败:状态{code=PERMISSION_DENIED,DESCRIPTION=缺少权限或权限不足,原因=空}

这是执行查询的代码。

class EmployeeList extends StatefulWidget {
  static bool isMale;
  final String userId;
  EmployeeList([this.userId]);
  void setIsMale(bool gen) {
    isMale = gen;
  }

  bool get getIsMale => isMale;

  @override
  State<StatefulWidget> createState() => _EmployeeList();
}

class _EmployeeList extends State<EmployeeList> {
  String employeeName;

  final TextEditingController textEditingController = 
TextEditingController();
  String gender;
  int keyIndex;
  CollectionReference listColRef;
  bool firstTime;
  Firestore db = Firestore.instance;

  @override
  void initState() {
    gender = EmployeeList().getIsMale ? "Male" : "Female";
    listColRef = 
db.collection('Users').document(widget.userId).collection('EmployeeList');
    print(widget.userId);
    super.initState();
  }

 @override
 Widget build(BuildContext context) {
   return Scaffold(
      backgroundColor: Colors.grey[100],
      appBar: AppBar(
         title: Text('Employees'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(5),
        child: StreamBuilder<QuerySnapshot>(
            stream: listColRef.where('Gender', isEqualTo: 
        gender).snapshots(),

这是数据库的结构:https://imgur.com/a/aI6UikO

推荐答案

您的数据库规则与您的查询不匹配。您的查询正在尝试使用模式/Users/{userId}/EmployeeList在集合下查找文档。但您的规则仅允许访问/Users/{documentID}下的文档。如果要允许访问嵌套子集合中的文档,则需要确保整个路径匹配。例如:

match /Users/{userId}/EmployeeList/{id} {
  allow read, write: if isOwner(userId);
}

这篇关于无法在FiRestore规则中将quest.auth.id与文档ID匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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