3
votes

I'm trying to add security rules to my firebase application. But auth.uid is not working..

Here is two of my security rules..

  "groups": {
      "$groupid":{
          ".read" : true,
          ".write" : "root.child('groupmembers').child($groupid).child('admins').child(root.child('users').child(auth.uid).child('username').val()).val()==true"
      }

    },
    "groupchat":{
      "$chatid":{

          ".read" : "root.child('groupmembers').child($chatid).child('members').child(root.child('users').child(auth.uid).child('username').val()).val()==true",
          ".write" : "root.child('groupmembers').child($chatid).child('members').child(root.child('users').child(auth.uid).child('username').val()).val()==true"

    }

  }

On the first rule auth.uid is working.. But in case of second and rest other rules, auth.uid is not working at all. I added my original uid in place of auth.uid and all rules are working properly.

Client side operation example..

 Map<String, Object> map = new HashMap<String, Object>();
 map.put("username", userid);
 map.put("message", userid + " Created This Group.");
 map.put("timestamp", ServerValue.TIMESTAMP);
 map.put("zcode",chat_key);
 map.put("type", "action");
 FirebaseDatabase.getInstance().getReference()
    .child('groupchat')
    .child(groupid)
    .push()
    .updateChildren(map);

Users Structure

"users":{
  "my_uid" : {
    "email" : "[email protected]",
    "username" : "user1"
  }
}

groupmembers structure

"groupmembers":{
  "groupid":{
    {
      "admins" : {
        "user1" : true,
        "user3" : true
      },
      "members" : {
        "user1" : true,
        "user2" : true,
        "user3" : true,
        "user4" : true
      }
    }
}
}

So how can I solve this problem?

1
Please share the minimal code that reproduces the problem. In addition to the code that fails, also include the existing, relevant JSON from your database (as text, no screenshots). You can get this by clicking the "Export JSON" link in your Firebase Database console. - Frank van Puffelen
Added the codes. But it's not the problem with only that rule. Except the first rule, in every rules auth.uid is behaving same... @FrankvanPuffelen - Bucky
I don't immediately see what's wrong with the rules, unless the groupid mismatches. But aside from that, maybe someone else spots the error. - Frank van Puffelen

1 Answers

0
votes

I think it was because of the version mismatch or different packages used for it so it can't can the auth.uid