Firebase安全和规则,如何让用户删除自己的数据?

use*_*835 2 firebase firebase-security firebase-realtime-database

我在firebase中的数据看起来像这样,在我的Web应用程序中,访问它的每个人都通过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"
  }
Run Code Online (Sandbox Code Playgroud)

我希望我的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()"
      }
    }
}
Run Code Online (Sandbox Code Playgroud)

Fra*_*len 6

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

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

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