0
votes

I have multiple root nodes and security rules for all those nodes are same as follows

".read": "auth.uid == uid"
".write": "auth.uid == uid"

Instead of repeating the same code again and again how to merge this following code?

{
"rules": {
    "node_1": {
       "$uid": {
           ".read": "auth.uid == uid"
           ".write": "auth.uid == uid"
      }
   },
   "node_2": {
      "$uid": {
          ".read": "auth.uid == uid"
          ".write": "auth.uid == uid"
      }
   },
   "node_3": {
      "$uid": {
          ".read": "auth.uid == uid"
          ".write": "auth.uid == uid"
      }
    }
  } 
}

enter image description here

fetchUserProfile = FirebaseDatabase.getInstance().getReference( Constants.FB_PROFILE_INFO ).orderByKey().equalTo( mAuth.getUid() );
1
Can you share what data is stored an who do want to give access to?Dharmaraj
Sub nodes of all root nodes are USER IDAkash Starvin Dsouza

1 Answers

0
votes

You can use a variable, just like you are now with $uid:

{
"rules": {
  "$node": {
     "$uid": {
       ".read": "auth.uid == $uid"
       ".write": "auth.uid == $uid"
     }
  }
}

This only make sense if you know, for certain, that all of the top-level nodes have the same structure. If they don't, you will perhaps need to organize them differently. There are no partial string matches for names of nodes.