Search code examples
firebasegoogle-cloud-firestorefirebase-security

How to access custom claims inside Cloud Firestore rules?


I have an user which has the following custom user claims,

 customClaims: { role: 'admin' },

How can I access this role property (admin) inside the cloud firestore rules?

I'm using the code below, which doesn't work. What needs to be done in order to work?

match /companies/{document=**} {
  allow read: if request.auth != null;
  allow write: if request.auth != null && request.customClaims.role == "admin";
}

Solution

  • Custom claims are in request.auth.token object as mentioned in the documentation:

    match /companies/{document=**} {
      allow read: if request.auth != null;
      allow write: if request.auth != null && request.auth.token.role == "admin";
    }