代码之家  ›  专栏  ›  技术社区  ›  Mrs.H

防止未登录用户阅读评论

  •  0
  • Mrs.H  · 技术社区  · 8 年前

    我有这个结构

    posts : {
        "id" : {
            title : "title" ,
            content : "content" ,
            image : "url" ,
            comments : {
                "commentId" : {
                    "name" : "name" ,
                    "comment" : "comment" ,
                    "time" : 26342624362436
                }
            }
        }
    }
    

    我想防止未登录用户看到帖子的评论,但他们可以看到其他信息(标题、内容、图像)

    以下是我的规则:

    {
        "rules" : {
            "posts" : {
                "$id" : {
                    ".read" : "auth != null"
                }
            }   
        }
    }
    

    但这将只允许登录用户阅读,非登录用户无法阅读帖子信息(标题、内容、图像)。

    有谁能帮我解决我的问题吗?

    1 回复  |  直到 8 年前
        1
  •  2
  •   Ali Faris    8 年前

    为了解决你的问题,我需要稍微改变一下你的结构

    posts : {
        "$id" : {
            "metaData" : {
                title : "title" ,
                content : "content" ,
                image : "url" ,
            }
            "comments" : {
                "$commentId" : {
                    "name" : "name" ,
                    "comment" : "comment" ,
                    "time" : 26342624362436
                }
            }
        }
    }
    

    然后你可以使用这些规则

    {
        "rules" : {
            "posts" : {
                "$id" : {
                    "metaData" : {
                        ".read" : "true"
                    },
                    "comments" : {
                        ".read" : "auth != null"
                    }
                }
            }   
        }
    }