【发布时间】:2018-05-31 02:03:00
【问题描述】:
我正在为 firebase 的云 Firestore 编写规则,并且我想使用用户的身份验证 ID 来限制对数据库的访问,但这些规则的行为与我预期的不同。
所以,我的数据库结构是这样的:
/users/{userId}/groups/{groupId}
我只希望用户只能使用他们自己的 userId 访问文档。
为此,我编写了如下规则:
service cloud.firestore {
match /databases/{database}/documents {
match /users/{userId=**} {
allow read: if userId == request.auth.uid;
}
}
}
但是有了这个,我的用户无法从数据库中读取他们自己的“组”。
我正在使用 javascript,检索“组”的代码如下:
console.log("uid: ", uid)
db.collection("users/" + uid + "/groups")
.onSnapshot(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
console.log(doc.data())
})
})
现在由于云火库没有像实时数据库那样好的调试工具,我自己做了一点调试。
我有一个 uid 为“5lS2NA21UgbabEw4AkyWyef9FH42”的测试用户,所以我更改了这样的规则:
service cloud.firestore {
match /databases/{database}/documents {
match /users/{userId=**} {
allow read: if "5lS2NA21UgbabEw4AkyWyef9FH42" == request.auth.uid;
}
}
}
有了这个,我的测试用户能够成功地从他的文档中检索“组”数据(当然所有其他用户都不能,但是)。
现在我将规则更改为:
service cloud.firestore {
match /databases/{database}/documents {
match /users/{userId=**} {
allow read: if "5lS2NA21UgbabEw4AkyWyef9FH42" == userId;
}
}
}
现在我无法从数据库中获取任何数据。
谁能告诉我我做错了什么?
【问题讨论】:
标签: javascript firebase firebase-security google-cloud-firestore