title: Firebase Database Rules - rules based on custom claims
body: <p>I have a firebase realtime database. Currently it contains two nodes <code>admin</code> and <code>common</code></p>
  
  <pre><code>{
    "admin" : {
      "adminval" : 9898574632,
      "adminval1" : 645354536,
      "adminval2" : 7776756433
    },
    "common" : {
      "commonval" : 123433221
    }
  }
  </code></pre>
  
  <p>I added to each user custom claim <code>roles</code> which describes roles user has in my system. It looks like this</p>
  
  <pre><code>{'roles': ['ROLE_ADMIN', 'ROLE_USER']}
  </code></pre>
  
  <p>Now I would like to restrict access so only users with claim <code>ROLE_ADMIN</code> are allowed to read/write <code>admin</code> node and with either of the roles can read/write node <code>common</code>.</p>
  
  <p>How to do it ? I tried something like it:</p>
  
  <pre><code>{
      "rules": {
         "admin": {
             ".read": "auth.token.roles.contains('ROLE_ADMIN')",
             ".write": "auth.token.roles.contains('ROLE_ADMIN')"
         }
         "common": {
             ".read": "auth.token.roles.contains('ROLE_USER') || auth.token.rules.contains('ROLE_ADMIN')",
             ".write": "auth.token.roles.contains('ROLE_USER') || auth.token.rules.contains('ROLE_ADMIN')"
         }
      }
    }
  </code></pre>