How to get newData key for Firebase Rules?

时间:2017-06-15 09:38:57

标签: android firebase firebase-authentication firebase-security

Here is very simple rule for users:

{
  "rules": {
    "users": {
      ".read": true,
      "$userId": {
        ".write": "auth.uid === $userId"
      }
    }
  }
}

Of course it's not working when we want to create new user, and here is my question, how to create rule which will be checking if new key in users path will be the same as auth.uid?

Update

I need something like this line ".write": "auth.uid === $userId" below ".read": true but here is impossible because it wouldn't see $userId variable.

In my case this should work:

  • Location: /users
  • Data: { "user1" : { "name": tester" } }
  • UID: user1

but this shouldn't:

  • Location: /users
  • Data: { "user1" : { "name": tester" } }
  • UID: user2

1 个答案:

答案 0 :(得分:0)

{
  "rules": {
    "users": {
      ".read": true,
      ".write": "newData.hasChild(auth.uid) || data.hasChild(auth.uid)",
        "$userId": {
          ".write": "auth.uid === $userId"
        }
    }
  }
}