简体   繁体   English

Firebase Firestore 安全规则 | 如果字段值与 auth uid 相同,则允许读取

[英]Firebase Firestore Securitty Rule | Allow read if field value same as auth uid

I want to read all document from我想阅读所有文件

在此处输入图片说明

using this query使用这个查询

QuerySnapshot data = await firestore
          .collection('Order')
          .where('uId', isEqualTo: auth.currentUser!.uid)
          .limit(20)
          .get();

and also want to apply security read only the requested auth uid same as uId/sellerUId.并且还想应用与 uId/sellerUId 相同的安全只读请求的 auth uid。

I already try with this security rule我已经尝试使用此安全规则

    match /Order/{uniqueCode} {
        allow read: if get(/databases/$(database)/documents/Order/$(uniqueCode)).data.sellerUId == request.auth.uid ||
                    get(/databases/$(database)/documents/Order/$(uniqueCode)).data.uId == request.auth.uid;
      allow write, update, delete: if true;
    }

I'm not sure if this is causing your problem, but you probably don't want to get() the same document that you're securing.我不确定这是否会导致您的问题,但您可能不想get()您正在保护的同一文档。 Instead you can use resource to refer to it:相反,您可以使用resource来引用它:

match /Order/{uniqueCode} {
    allow read: if resource.data.sellerUId == request.auth.uid ||
                   resource.data.uId == request.auth.uid;
    allow write, update, delete: if true;
}

You don't need to specify the complete path if the fields you want to compare with are already in document.如果要比较的字段已在文档中,则无需指定完整路径。

match /Order/{uniqueCode} {
      allow read: if resource.data.sellerUId == request.auth.uid || resource.data.uId == request.auth.uid;
      allow write, update, delete: if true;
}

You can read more about data validation here您可以在此处阅读有关数据验证的更多信息

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

相关问题 基于属性值的Firebase Firestore安全性规则 - Firebase Firestore Securitty Rule based on Attribute Value auth.uid的Firebase规则 - Firebase Rule for auth.uid Firestore 规则,request.auth.uid 与我从 firebase auth google signin 获得的 uid 不同 - Firestore Rules, request.auth.uid is not the same as uid I get from firebase auth google signin Firebase 规则 - 如果用户的 uid 等于文档的字段值 userId,则允许读取 - Firebase rules - Allow read if user's uid equals document's field value userId Flutter - Firebase Auth UID 在 Firestore 中注册为 Null - Flutter - Firebase Auth UID registers as Null in Firestore FireBase 规则不适用于 auth.uid - FireBase Rule not working with auth.uid 只读列表中具有值为auth.uid的字段的子级 - Read only children of a list that have a field with the value of auth.uid firebase“ .read”:“ auth!= null && auth.uid == $ uid”无法正常工作 - firebase “.read”: “auth != null && auth.uid == $uid” not working as expected 如何匹配存储在 Firestore 的 Firebase 安全规则内的文档字段中的用户 uid - How to match a user's uid stored in document field inside Firebase security rule for Firestore 如何允许访问需要身份验证 uid、firebase 数据库规则的字段中的特定字段 - How to allow access to a specific field inside of one that requires auth uid, firebase database rules
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM