简体   繁体   English

如何匹配存储在 Firestore 的 Firebase 安全规则内的文档字段中的用户 uid

[英]How to match a user's uid stored in document field inside Firebase security rule for Firestore

I want to check for the UID stored in the document field of a document (of the parent collection named premiumID ) against the UID of the user requesting the data.我想根据请求数据的用户的 UID 检查存储在文档(名为premiumID的父集合)的文档字段中的 UID。 In simpler words, I want the user to only access the document which contains his UID.简单来说,我希望用户只访问包含他的 UID 的文档。

Here's what I have done until now这是我到目前为止所做的

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

    match /premiumID/{docId} {

      function userData() {
        return resource.data.uid
      }

      allow update: if userData() == request.auth.uid;
      allow delete: if userData() == request.auth.uid;
      allow create: if request.auth.uid != null;
      allow read: if userData() == request.auth.uid;
    }    
  }
}

Here's what my DB looks like-这是我的数据库的样子-

在此处输入图片说明

Here's how I am testing it.这是我测试它的方式。 This gives me read and write denied .这让我读写被拒绝

在此处输入图片说明

The rule allow write: if request.auth.uid == request.resource.data.uid;规则allow write: if request.auth.uid == request.resource.data.uid; checks if user authentication is the same as the field uid that the document contains.检查用户身份验证是否与文档包含的字段uid相同。 But as said in the comments of your question your collection name doesn't match the rule that you are using.但正如您的问题评论中所说,您的集合名称与您使用的规则不匹配。

Check this site it contains very useful information about firebase rules.检查此站点,它包含有关 Firebase 规则的非常有用的信息。 It really helped me when I started using firebase rules for more complicated rules.当我开始使用 firebase 规则来处理更复杂的规则时,它真的帮助了我。

I think if you want to match a document you have to use {document} instead of {docId} .我想如果你想匹配一个文档,你必须使用{document}而不是{docId} I also had similar problem and changing to {document} solved it.我也有类似的问题,改成{document}解决了。

match /pID/{document}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Firestore 中,如何获取 Firebase Authentication 的 uid 为文档? - In Firestore, how to get the uid of Firebase Authentication to be a document? 如何在 Firebase Firestore 中添加自动 uid 字段 - How to add auto uid field in Firebase Firestore Firebase:如何获取其他用户的uid? - Firebase : How to get other user's uid? 用户 UID 与 Firestore Android 中的文档 ID 不同 - The User UID is not the same with document id in Firestore Android 如何在不使用firebase身份验证的情况下编写firestore数据库安全规则 - How to write firestore database security rule while not use firebase authentication 如何检测firestore中文档内的字段删除? - How to detect a field deletion inside a document in firestore? 如何在 queryDocumentSnapshot Firestore 中查询文档字段 - How to query a document field inside of queryDocumentSnapshot Firestore 给定特定的经过身份验证的用户 (uid) 的 android 应用程序,无法为顶级集合(列表权限)编写 Firestore 安全规则 - Can not write firestore security rule for top level collection (list permission) given a specific authenticated user (uid) for an android app 如何在firestore文档的字段中搜索 - how to search at firestore document's field 如何编写 firebase 实时数据库规则以匹配根文件夹子文件夹名称与新数据自定义 uid - how to write a firebase Realtime database rule to match root folder child folders name with New Data custom uid
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM