Firebase protection permission not working

I have data stored in my firebase:

firebaseRoot
    admins
        simplelogin:1: 
    users
        simplelogin:1
            email: abc@xyz.com
            picture: csd
            provider: password
            uid: simplelogin:1
        simplelogin:2
            email: abc1@xyz.com
            picture: zsd
            provider: password
            uid: simplelogin:1

and the following safety rules:

{
  "rules": {
    "admins": {
      ".read": "root.child('admins').child(auth.uid).val() === true",
      ".write": "root.child('admins').child(auth.uid).val() === true"
    },
    "users": {
      "$user":{
        ".read": "$user === auth.id || root.child('admins').child(auth.uid).val() === true",
        ".write": "$user === auth.id"
      }
    }
  }
}

My authorization requirements are listed below.

  • Administrators
  • can only be read and added by an existing administrator. It works.
  • All users can be read by the administrator, but cannot write user data.
  • The user can read and update their own user data.

Currently, with the above rules, I cannot read user data for both administrators and registered users. I get below error message. Please provide your help. Thank.

var rootRef = new Firebase('https://xxxxx.firebaseio.com/');
var users = rootRef.child('users');
users.on('value', function(snap) {
console.log(snap.key(), snap.val());
}, function(error) {
console.log(error);
});

Error:

Error: permission_denied: the client does not have permission to access the required data.

+4
2

Firebase :

  • , , - ( ) JSON,

  • , node, node. , .

( ) users. users.on('value' .

, .read users node.

    "users": {
      ".read": "root.child('admins').child(auth.uid).val() === true",
      "$user":{
        ".read": "$user === auth.id",
        ".write": "$user === auth.id"
      }
    }
+3

:

ajsecuretest
    roles
        simplelogin:1
            role: 'admin'
        simplelogin:2
            role: 'editor'
    users
        simplelogin:1
             email: 'abc@xyz.com'
             picture: 'a.jpg'
             provider: 'password'
             uid: 'simplelogin:1'
        simplelogin:2
             email: 'xyz@abc.com'
             picture: 'b.jpg'
             provider: 'password'
             uid: 'simplelogin:2'

:

{
  "rules": {
    "roles":{
      ".read": "root.child('roles').child(auth.uid).child('role').val() === 'admin'",
      ".write": "root.child('roles').child(auth.uid).child('role').val() === 'admin'",
      "$id" : {
        ".read" : "$id === auth.uid"
      }
    },
    "users": {
      ".read" :  "root.child('roles').child(auth.uid).child('role').val() === 'admin'",
      "$user":{        
        ".read": "$user === auth.uid",
        ".write": "$user === auth.uid"
      }
    }
  }
}
+1

All Articles