无法为 Firebase 子集合和角色创建正确的规则



我正在创建一个集成了Firebase的离子应用程序,其中我在应用程序中创建了一个帖子部分。 用户可以像Facebook一样撰写帖子,任何经过身份验证的用户都可以对这些帖子发表评论, 以下是我如何存储数据的数据库设计:- posts/{postsDocument}/comments/{commentsDocument} 评论是帖子文档的子集合,其中包含特定帖子的所有评论。此外,postDocument和commentsDocument包含用户的Firebase uid。 我写了规则,任何用户都可以对帖子发表评论并阅读其他人的评论,但我想要用户可以更新自己的评论而不是其他人的规则,也只有帖子所有者可以删除评论。 检查以下规则。请帮助我解决我所缺少的问题

match /posts/{postId} {
allow read, write: if request.auth.uid != null;
match /{comments}/{commentId} {
allow read, create: if request.auth.uid != null;
allow update: if ;
allow delete: if ;
}
}

检查以下规则以适合您的情况

match /posts/{postId} {
allow read, write: if request.auth.uid != null;
match /{comments}/{commentId} {
allow read, create: if request.auth.uid != null;
allow update: if resource.data.commentOwnderId == request.auth.uid;//commentOwnderId, firebase uid of the user who wrote the comment
allow delete: if get(/databases/$(database)/documents/posts/$(postId)).data.postOwnerId == request.auth.uid;//postOwnerId, firebase uid of the user who wrote the post
}
}

最新更新