Firebase 实时数据库安全规则

时间:2020-12-22 13:03:57

标签: firebase-realtime-database firebase-authentication firebase-security

当用户被禁止进入房间时,我想删除这些用户对安全规则的读写权限。顺便说一句:我将用户 ID 添加到房间中,以检查该用户是否被禁止。

安全规则;

{
  "rules": {
    "live": {
        "rooms": {
          "$room": {
              ".write": "!$room.child('auth.uid').exists()", // auth.uid from Fb Auth
              ".read": "!$room.child('auth.uid').exists()",
            }
        }
    }
  }
}

实时数据库数据

{
  "live" : {
    "rooms" : {
      "1245" : { // room id
        "14Kq4sx2X07FbNhOVBxQcYM3TZ8X53" : -1, // banned user uid from Auth
        "comments" : [ {
          "comment" : "hello",
          "date" : "2020-12-20 20:23:33",
          "uid" : "14Kq4sx2X07FbNhOVBxQcYM3TZ8X53",
          "username" : "name"
        } ]
      }
    }
  }
}

但是这些规则不起作用,当我在房间中添加了 auth id 但每个用户都可以读写这个房间时

1 个答案:

答案 0 :(得分:1)

您在 'auth.uid' 周围有引号,这意味着规则会检查该文字字符串:'auth.uid'。如果您想再次检查 auth.uid 的值,则不应将其放在引号中。

所以:

".write": "!data.child(auth.uid).exists()",