【发布时间】:2017-07-26 08:44:02
【问题描述】:
我希望我的用户能够使用我的应用添加posts。我需要知道每个帖子的发帖人,以便客户使用
this.firebaseapp.database().ref('posts/').push({
userId, text, timestamp:new Date()
});
但从技术上讲,有人可以修改客户端并为userId 发送任何值,所以我需要一个数据库规则来防止这种情况发生。
Firebase 网站上的用户数据示例提到:
{
"rules": {
"users": {
"$user_id": {
// grants write access to the owner of this user account
// whose uid must exactly match the key ($user_id)
".write": "$user_id === auth.uid"
}
}
}
}
我想我基本上想要 .write 限制,但由于 Posts 是不同的数据集,我不想使用 Post 键,我想引用正在传入的值。
这是我尝试过的,但它仍然可以写帖子:
{
"rules": {
".read": "auth != null",
".write": "auth != null",
"posts": {
"userId": {
".write": "newData.val() === auth.uid"
}
}
}
}
【问题讨论】:
标签: javascript firebase firebase-realtime-database firebase-security