Home > front end >  How to access custom claims inside Cloud Firestore rules?
How to access custom claims inside Cloud Firestore rules?

Time:10-12

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";
}

CodePudding user response:

All claims (including custom ones) are available under the request.auth.token variable. Note that it may take up to an hour before the claims propagate to the security rules, as they are embedded in the user's ID token.

Also see:

CodePudding user response:

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";
}
  • Related