首页 文章

Firebase安全规则?

提问于
浏览
3

我似乎很难使用firebase安全规则 . 我已经阅读了这些指南,但是模拟器的结果不够具有描述性(如果我们可以将鼠标悬停在某个节点上,并且弹出一个可以更新规则的按钮,则会更容易) .

这是我的结构:

chats
  - randomChatId01
    - name: "awesome chat"
    - members:
        - userId01 : true
        - userId02 : true
  - randomChatId02
    - members:
        - userId02 : true
  - randomChatId03
    - members:
        - userId01 : true
        - userId02 : true
  - ...

我只希望用户能够读取节点的子节点成员包含经过身份验证的用户的auth.uid的聊天节点 .

因此,在这种情况下,如果userId01已登录,则她只能读取randomChatId01和randomChatId03的读取权限 .

这是我的规则:

{
    "rules": {

        "chats": {

          "$chat": {
            ".read": "data.child('members').val().contains(auth.uid)"
          }
        }
    }
}

然而,它在模拟器中返回以下内容:

Attempt to read /chats with auth={"provider":"anonymous","uid":"eF4ztDEXz7"}
    /
    /chats

No .read rule allowed the operation.
Read was denied.

1 回答

  • 5

    这是因为Firebase安全规则是在您读取的位置进行评估的 .

    你正试图阅读 /chats . 用户没有 /chats 的读取权限,因此操作立即失败 .

    如果您将 /chats/randomChatId01 读为 userId01 则会成功 .

    这将在文档部分rules are not filters中介绍 . 另请参阅Michael Lehenbauer的答案:Restricting child/field access with security rules

相关问题