您是否将auth.uid 设置为文档ID,并将对象键命名为userID?
Firestore.instance.collection("debtors").doc("7ScK2T0T3SMUR1NJxjiCiRzTnm62").set({
firstname: "Aacis",
relation: "friend",
userID: "7ScK2T0T3SMUR1NJxjiCiRzTnm62"
})
如果您只想允许 onwer 读取访问权限,则应设置allow read: if request.auth.uid != null && request.auth.uid == userID;
如果你想读取匹配 auth.uid == resource.data.userID 的文档,那么你应该设置allow read: if request.auth.uid != null && request.auth.uid = resource.data.userID;
如果你想允许公众阅读,那么你应该设置allow read: if true;
此外,读取规则可以分为 get 和 list。
service cloud.firestore {
match /databases/{database}/documents {
// A read rule can be divided into get and list rules
match /cities/{city} {
// Applies to single document read requests
allow get: if <condition>;
// Applies to queries and collection read requests
allow list: if <condition>;
}
// A write rule can be divided into create, update, and delete rules
match /cities/{city} {
// Applies to writes to nonexistent documents
allow create: if <condition>;
// Applies to writes to existing documents
allow update: if <condition>;
// Applies to delete operations
allow delete: if <condition>;
}
}
}
见:
更新
下面代码中的userID表示文档id为auth.uid,只允许onwer读取权限。
match /debtors/{userID}/{documents=**} {
allow read: if request.auth.uid != null && request.auth.uid == userID;
所以你不要将auth.uid设置为文档id,并且文档中名为userID的存储值匹配auth.uid,你应该使用resource.data.userID和request.resource.data.userID。
请尝试以下代码。
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /debtors/{document=**} {
allow read: if request.auth.uid != null && request.auth.uid == resource.data.userID;
allow create: if request.auth.uid != null && request.auth.uid == request.resource.data.userID;
allow update, delete: if request.auth.uid != null && request.auth.uid == resource.data.userID;
}
}
}
资源变量引用请求的文档,resource.data 是存储在文档中的所有字段和值的映射。有关资源变量的更多信息,请参阅参考文档。
写入数据时,您可能希望将传入数据与现有数据进行比较。在这种情况下,如果您的规则集允许挂起写入,则 request.resource 变量包含文档的未来状态。对于只修改文档字段子集的更新操作,request.resource 变量将包含操作后的待处理文档状态。您可以检查 request.resource 中的字段值以防止不需要或不一致的数据更新:
见: