[英]Missing or insufficient permissions when writing to Firestore using field in access rules
尝试写入 Firestore 时出现错误。
我正在尝试将包含用户 uid 的字段用于我的安全规则。
service cloud.firestore {
match /databases/{database}/documents {
match /messages/{document=**} {
allow read: if resource.data.user_uid == request.auth.uid;
allow write: if resource.data.user_uid == request.auth.uid;
}
}
}
如果我的 Firestore 数据库中有数据,则读取规则工作正常 - 但是当我尝试写入时,我得到: Error: Missing or insufficient permissions.
这个写入规则有什么问题吗?
PS如果我将规则更改为此,我可以写入我的数据库 - 但这对我的目的来说不够安全:
match /messages/{document=**} {
allow read: if resource.data.user_uid == request.auth.uid;
allow write: if request.auth != null;
}
resource.data
是指已存储的数据,因此您的规则允许用户仅更新已包含其用户 ID 的数据。
您可能想要检查的是request.resource.data
,这是request.resource.data
的新数据。
这里有一份关于这些字段和其他安全规则的相当广泛的文档: https : //firebase.google.com/docs/firestore/security/rules-conditions
这个问题和公认的答案对我非常有用。 我最终使用了一组略有不同的规则,我将在这里分享,以防其他人发现它们有用。
与 OP 一样,我将用户 ID ( uid
) 与我的资源一起存储。 但是,当我想创建新资源时,还没有uid
。 使用文档中的示例,我最终得到了如下所示的安全规则:
service cloud.firestore {
match /databases/{database}/documents {
match /messages/{document=**} {
allow read, update, delete: if request.auth.uid == resource.data.uid;
allow create: if request.auth.uid != null;
}
}
}
您可以使您的数据库仅用于读取而不是用于写入:
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read: if true;
allow write: if false;
}
}
}
对我来说,这组代码适用于 Firestore 安全
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read,write: if true;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.