Firebase 只允许创作者 post
Firebase allowing only the creator to post
我一直在为我的个人网站开发博客应用程序,我只想允许自己 post 新博客 post,但允许所有登录用户添加评论 posts.
目前数据如下所示:
"posts": {
"-KpZlH9PYs7mAf8avBmI" : {
"comments" : {
"-KpZwWIrbM3JQ2ug1c5_" : {
"message" : "How are you today?",
"timestamp" : 1500637173055,
"user" : "Florin Pop"
},
"-KpZyxoC0OTxnDZymP-M" : {
"message" : "I'm fine, thank you!",
"timestamp" : 1500637814102,
"user" : "Florin Pop"
}
},
"likes" : 0,
"text" : "asxszx",
"timestamp" : 1500634227427,
"title" : "qwqew"
}
}
如您所见,post 有:评论列表、赞、文本、时间戳 和标题。
我不确定结构是否最符合我的要求。
有什么方法可以检查我是否是当前登录的用户,然后才允许创建新的 post?
我应该单独评论吗?
P.S.
目前我的数据库规则是:
{
"rules": {
".read": true,
".write": "auth != null"
}
}
Should I have the comments separately?
即使不知道您应用的所有细节,答案也几乎可以肯定 "yes"。在不同的顶级节点中拥有不同的实体类型通常会大大简化数据库的安全规则。
Is there way I can check if I'm the currently logged in user.
在安全规则中,您可以检查操作是否来自 a 身份验证用户 auth != null
。但是据我所知,您的 JSON 不包含 UID,因此我不清楚 当前用户是谁。
我建议您进行以下更改:
在数据库的根目录下创建一个新对象admins
,可以edit/create发帖的用户:
"admins": {
"<ADMIN_1_UID>": "true",
"<ADMIN_2_UID>": "true"
}
然后像这样更改您的安全规则:
"rules": {
"admins": {
".read": false,
".write": false /*This ensures that only from firebase console you can add data to this object*/
},
"posts": {
".read": true,
".write": "root.child('admins').child(auth.uid).val()" /*This ensures only admin can write post stuff*/
"$postId": {
"comments": {
".write": "auth != null" /*This overrides the above write rule and allow authenticated users to post comments*/
}
}
}
}
我一直在为我的个人网站开发博客应用程序,我只想允许自己 post 新博客 post,但允许所有登录用户添加评论 posts.
目前数据如下所示:
"posts": {
"-KpZlH9PYs7mAf8avBmI" : {
"comments" : {
"-KpZwWIrbM3JQ2ug1c5_" : {
"message" : "How are you today?",
"timestamp" : 1500637173055,
"user" : "Florin Pop"
},
"-KpZyxoC0OTxnDZymP-M" : {
"message" : "I'm fine, thank you!",
"timestamp" : 1500637814102,
"user" : "Florin Pop"
}
},
"likes" : 0,
"text" : "asxszx",
"timestamp" : 1500634227427,
"title" : "qwqew"
}
}
如您所见,post 有:评论列表、赞、文本、时间戳 和标题。
我不确定结构是否最符合我的要求。
有什么方法可以检查我是否是当前登录的用户,然后才允许创建新的 post?
我应该单独评论吗?
P.S.
目前我的数据库规则是:
{
"rules": {
".read": true,
".write": "auth != null"
}
}
Should I have the comments separately?
即使不知道您应用的所有细节,答案也几乎可以肯定 "yes"。在不同的顶级节点中拥有不同的实体类型通常会大大简化数据库的安全规则。
Is there way I can check if I'm the currently logged in user.
在安全规则中,您可以检查操作是否来自 a 身份验证用户 auth != null
。但是据我所知,您的 JSON 不包含 UID,因此我不清楚 当前用户是谁。
我建议您进行以下更改:
在数据库的根目录下创建一个新对象admins
,可以edit/create发帖的用户:
"admins": {
"<ADMIN_1_UID>": "true",
"<ADMIN_2_UID>": "true"
}
然后像这样更改您的安全规则:
"rules": {
"admins": {
".read": false,
".write": false /*This ensures that only from firebase console you can add data to this object*/
},
"posts": {
".read": true,
".write": "root.child('admins').child(auth.uid).val()" /*This ensures only admin can write post stuff*/
"$postId": {
"comments": {
".write": "auth != null" /*This overrides the above write rule and allow authenticated users to post comments*/
}
}
}
}