我在我的 firebase 数据库中添加安全规则时遇到问题。如果经过身份验证的用户等于投票文档上的 uid 属性,我只想允许删除投票。除了我在匹配/votes/{voteId} 上创建的删除规则之外,一切正常。
我尝试使用 resource.data.uid 执行此操作,但模拟器提示并收到错误“运行模拟时出错 - 错误:simulator.rules Null 值错误”
service cloud.firestore {
match /databases/{database}/documents {
match /polls/{pollId} {
allow read;
allow delete: if getUserData().roles.keys().hasAny(['admin']);
allow create: if isSignedIn();
}
match /users/{userId} {
allow read, write: if isOwner(userId);
}
match /votes/{voteId} {
allow read;
allow create: if isSignedIn();
allow delete: if request.auth.uid == resource.data.uid;
}
}
/// Functions ///
function isSignedIn() {
return request.auth != null
}
function isOwner(userId) {
return request.auth.uid == userId
}
}
更新
我也尝试使用/{document=**} 通配符,它给了我同样的空值错误
match /votes/{document=**} {
allow read;
allow create: if isSignedIn();
allow delete: if request.auth.uid == resource.data.uid;
}
我也尝试使用 get() 函数,但收到错误“找不到函数错误:名称:[get]
match /votes/{voteId} {
allow read;
allow create: if isSignedIn();
allow delete: if get(/databases/$(database)/documents/votes/$(voteId)).data.uid == request.auth.uid
}
最佳答案
您是否创建了一个名为 uid
的字段的文档? ?像这样。firebase.firestore().collections("votes").add({uid: firebase.auth().currentUser.uid});
resource
是一个firestore文件。resource.data
是文档数据的映射。
firebase 控制台上的模拟器正在使用您项目中存在的真实 firestore 数据。
而且我认为将规则更改为以下规则会更好。
...
match /votes/{voteId} {
allow read;
allow create: if isSignedIn() && request.auth.uid == request.resource.data.uid;
allow delete: if isSignedIn() && request.auth.uid == resource.data.uid;
}
...
function isSignedIn() {
return request.auth.uid != null
}
...
看:
关于Firebase 数据库规则,仅当文档属性等于经过身份验证的用户 ID 时才允许删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55841615/