Ad

Firebase Security Rules Access NewData Node Name

I couldn't find any way to access the name of the data that is being written in Firebase Rules, which is very critical for me. Does Firebase Rules supports such thing?

"tests":{
    "$test":{

      ".read": "auth.uid != null",
     ".write": "auth.uid  != null
         "$user_id":{".validate": "newData.isBoolean() && auth.uid == NewData.NAME(?)"}

    }
  }

Sample data that is gonna be written:

{
  "YHer1txgZyReM8dEkSCTDg4l2ro1": true
}

Sample database structure:

"tests":{
    "test1":{

       "YHer1txgZyReM8dEkSCTDg4l2ro1": true,
       "A4sfgYhred1festfsefsSRGFsgr3": true,
       "7JIsfgYhred1festfsefsSRGFsg8": true,

    }
 "test2":{

      "YHer1txgZyReM8dEkSCTDg4l2ro1": true,
      "A4sfgYhred1festfsefsSRGFsgr3": true,
      "7JIsfgYhred1festfsefsSRGFsg8": true,

    }
  }
Ad

Answer

Instead of newData.NAME(?) use $user_id.

So:

 "tests":{
    "$test":{

      ".read": "auth.uid != null",
     ".write": "auth.uid  != null
         "$user_id":{".validate": "newData.isBoolean() && auth.uid == $user_id"}"

    }
  }

Also see the Firebase documentation on securing user data, which contains an example that is very close to what you need.

Ad
source: stackoverflow.com
Ad