LoginSignup
0
0

More than 3 years have passed since last update.

Firebase Realtime DatabaseでUserIDフィールドの値を本人のUserIDに限定するrule

Posted at

ユーザーID自体はContextから取れるし、編集が可能なデータかどうかなどによって考慮点が増えるので、あんまり単体では意味のない知識だけど、ある日役に立ったので書き残しておく。

rule.json
{
  "rules": {
    ".read": false,
    ".write": false,
    "hoge" : {
      ".read" : false,
      ".write" :true,
      "$req_id" : {
        "userID": {
          ".validate" : "newData.val() === auth.uid"
        },
        ".validate": "newData.hasChildren(['userID'])"
      }
    }
  }
}

これでどうなるのか

この時のuserIDの値を本人のauth.uidに限定できる。

data.json
{
  "hoge" : {
    "data1" : {
      "city": "tokyo",
      "userID": "taro",
    },
    "data2" : {
      "city": "osaka",
      "userID": "jiro",
    }
  }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0