【问题标题】:Firebase Security & Rules, How can I let users delete their own data?Firebase 安全和规则,如何让用户删除自己的数据?
【发布时间】:2016-09-13 01:05:19
【问题描述】:

我在 firebase 中的数据如下所示,在我的网络应用程序中,访问它的每个人都通过 firebase 进行匿名身份验证,并且他们的 UID 与用户创建的每个帖子一起存储:

  "-KF5N2V_dKD1dMHebUqc" : {
    "note" : "Hello everybody",
    "pos" : {
      "lat" : 40.3628851,
      "lng" : -74.0493175
    },
    "time" : 1460395853884,
    "uid" : "f8cf7863-5607-4e2b-97d7-6a121261466c"
  },
  "-KHwyP-tnWNOA3nxzEm4" : {
    "note" : "hi",
    "pos" : {
      "lat" : 37.0947156,
      "lng" : -121.0179501
    },
    "time" : 1463459362615,
    "uid" : "f8cf7863-5607-4e2b-97d7-6a121261466c"
  }

我想要设置我的 firebase 规则,以便只有匿名用户可以通过自己的帖子删除他们的帖子。

到目前为止,我只是在阅读了 firebase 文档后才想到这个:

{
    "rules": {
      ".read": "auth != null",
      ".write": "auth != null",
      "$msg": {
        ".validate": "newData.hasChildren(['note','time','uid','pos']) 
          && newData.child('note').isString() && newData.child('time').isNumber() 
          && newData.child('uid').isString() && newData.child('note').isString()
          && newData.child('pos/lat').isNumber() && newData.child('pos/lng').isNumber()"
      }
    }
}

【问题讨论】:

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


    【解决方案1】:

    您需要下移.write 权限并将其绑定到数据:

    {
        "rules": {
          ".read": "auth != null",
          "$msg": {
            ".write": "!data.exists() || (!newData.exists() && data.child('uid').val() === auth.uid)"
            ".validate": "..."
          }
        }
    }
    

    Firebase 文档的这两个部分有点混搭:

    【讨论】:

    • 注意:如果您想模拟这是否有效,请确保您的位置在末尾包含 $msg 变量,例如 /msg。我一直因为试图写入 root / { "some-msg":"bar" } 而被拒绝权限
    猜你喜欢
    • 2021-06-10
    • 1970-01-01
    • 1970-01-01
    • 2019-09-21
    • 1970-01-01
    • 2018-11-27
    • 1970-01-01
    • 2017-06-24
    • 2016-02-25
    相关资源
    最近更新 更多