Search code examples
iosfirebase-realtime-databasefirebase-security

Firebase database rule for auto key names


I have a firebase database that looks like this

     Cards

         Group1
                    - 0
                         - isLight
                         - likes
                         - shares
                    - 1
                         - isLight
                         - likes
                         - shares

i need to write a rule that permits writing likes and shares only and close it for everything else , with read allow , so i wrote


{
  "rules": {  
    "Cards": {   
      ".write": "false" 
      "Group1" : { 
        "0": {
                  "likes" : { 
                      ".read": "true",
                      ".write": "true" 
                   }, 
                  "shares" : { 
                       ".read": "true",
                       ".write": "true" 
                  }
            } 
         } 
      }
   }
}

I don't want to statically write Group1 and 0 as there are no limit to their count so i need to substitute them with something general but i don't know it


Solution

  • Can you pls try it with this:

    {
      "rules": {  
        "Cards": {    
          "$Group" : { 
            "$index": {
                      "likes" : { 
                          ".read": "true",
                          ".write": "true" 
                       }, 
                      "shares" : { 
                           ".read": "true",
                           ".write": "true" 
                      }
                } 
             } 
          }
       }
    }