尝试写入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.
我写这条规则有什么问题吗?
附:如果我将规则更改为此,我可以写入我的数据库 - 但这对我的目的来说不够安全:
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
这是新数据的来源。
这里有关于这些字段和其他安全规则的相当广泛的文档: https://firebase.google.com/docs/firestore/security/rules-conditions
resource.data
指的是已存储的数据,因此您的规则允许用户仅更新已包含其用户ID的数据。
你可能想要检查的是 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;
}
}
}