Firebase Custom Claims Guide: Roles, Token Refresh, Admin SDK Workflows, and Authorization Checks
Firebase AuthenticationCustom ClaimsRBACAdmin SDKSecurity Rules

Firebase Custom Claims Guide: Roles, Token Refresh, Admin SDK Workflows, and Authorization Checks

FFirebase Live Editorial Team
2026-11-18
6 min read

Learn how to design Firebase custom claims, refresh tokens, enforce roles, remove access safely, and review authorization over time.

Firebase custom claims provide a practical way to attach roles and coarse-grained permissions to Firebase Authentication tokens. This guide shows how to design claims, update them with the Admin SDK, handle token refresh, enforce authorization in Security Rules and trusted servers, remove access safely, and review the setup as your application changes.

Overview

Role-based access control in Firebase usually has three connected parts: an identity in Firebase Authentication, authorization data issued as custom claims, and checks that enforce those claims. A typical flow looks like this:

  1. An administrator changes a user’s role in a trusted backend.
  2. The backend writes custom claims with the Firebase Admin SDK.
  3. Firebase issues the updated claims in a new ID token.
  4. The client uses the token for display logic, while Security Rules or a trusted server make the actual access decision.

Custom claims are best suited to small, stable authorization facts such as role: "editor", tenantId: "tenant-123", or billingAdmin: true. They are not a replacement for a full permissions database. Keep the payload compact, avoid putting secrets or personal data in it, and design the authorization model before adding individual flags.

Claims are carried in ID tokens, so a change is not necessarily visible to a client immediately. This is the source of many “the role changed, but access did not” reports. The claim update, token refresh, client state, and enforcement layer must all be considered together.

What to track

1. A role-and-permission matrix

Start with a table that separates business roles from the actions each role may perform. For example:

RoleRead projectsEdit projectsManage membersDelete projects
ViewerYesNoNoNo
EditorYesYesNoNo
AdministratorYesYesYesYes

Use one canonical role where possible instead of combining many overlapping booleans. If a user can belong to several organizations, do not assume one global role is enough; consider a tenant-aware data model and enforce membership against the relevant resource.

2. Claim structure and size

Track which claims are authoritative, who can change them, and where they are checked. A compact structure might look like:

{
  "role": "editor",
  "tenantId": "tenant-123"
}

Custom claims must remain small. Firebase documents a 1000-byte limit for the custom claims payload, so do not store project lists, profile records, or large permission arrays in the token. Store detailed or frequently changing authorization data in Firestore or another backend, then check it in a trusted server or model it carefully in Security Rules.

3. The token lifecycle

Document the lifecycle for your team:

Admin SDK updates claims
        |
        v
New ID token is issued or requested
        |
        v
Client sends token to Firebase service or backend
        |
        v
Security Rules or server verifies claims
        |
        v
Access is allowed or denied

A client can display a role from its current token, but that display is not an authorization boundary. Firestore, Realtime Database, and Storage Rules should enforce access at the resource. A custom backend should verify the Firebase ID token with the Admin SDK before trusting its claims.

4. The update and removal path

Only trusted server code should assign or remove privileged claims. Never let a browser decide that its own account is an administrator. Record the operator, affected user, previous role, new role, and reason in an audit trail appropriate to your application.

For Node.js, an update can follow this pattern:

const user = await getAuth().getUser(uid);

await getAuth().setCustomUserClaims(uid, {
  ...(user.customClaims || {}),
  role: "editor"
});

When removing a role, replace the claims with the intended remaining set rather than accidentally preserving an elevated flag. To remove all custom claims, use null with the Admin SDK method. Treat claim replacement as an explicit operation and test it with a user who previously had access.

Cadence and checkpoints

Review custom claims at least quarterly and whenever your authorization model changes. A useful checkpoint list is:

  • Role design: Are roles still understandable, mutually consistent, and no broader than necessary?
  • Token size: Has anyone added arrays, metadata, or copied profile data to claims?
  • Refresh behavior: Do clients request a new token after an administrative role change?
  • Rules enforcement: Do every relevant Firestore, Realtime Database, and Storage rule set check the intended claim?
  • Backend verification: Does every privileged server endpoint verify the ID token rather than trusting client-supplied role fields?
  • Removal and downgrade: Have revoked users been tested, including users with an already issued token?
  • Test coverage: Are allow and deny cases represented in Emulator Suite tests or an equivalent automated test suite?

Before a release, test at least a signed-out user, a newly promoted user, a downgraded user, a user with an expired session, and a user attempting to access another tenant’s resource. The Firebase Security Rules Audit can help turn these cases into a repeatable review. For local validation, pair claim fixtures with the Firebase Emulator Suite Testing Guide.

How to interpret changes

If an administrator updates a claim but the application still shows the old role, first inspect the client’s current ID token rather than the user profile. The token may simply be stale. In web applications, request a forced refresh after a known role change:

await auth.currentUser?.getIdToken(true);

The user may also receive new claims after signing in again or when the normal token lifecycle issues a new token. A forced refresh should be used deliberately, not on every render or request, because repeated refreshes create unnecessary traffic and complicate client behavior.

If refreshing does not help, follow this troubleshooting sequence:

  1. Confirm the Admin SDK update completed and targeted the expected user ID.
  2. Inspect the decoded ID token in a safe development workflow; do not paste production tokens into public tools.
  3. Check that the client is authenticated as the same user whose claims were changed.
  4. Verify that the Security Rule reads the exact claim path, such as request.auth.token.role.
  5. Check whether a backend endpoint is reading a stale cached user object or accepting a role from the request body.
  6. Test both the positive and negative path in the Emulator Suite or a controlled environment.

For Firestore Rules, a role check may look like:

match /projects/{projectId} {
  allow read: if request.auth != null;
  allow update: if request.auth.token.role in ["editor", "administrator"];
  allow delete: if request.auth.token.role == "administrator";
}

Rules syntax and product behavior can evolve, so verify examples against the Firebase documentation and your deployed Rules before treating a snippet as production-ready. Also remember that a client-side role label is only presentation; the rule or verified backend decision is what protects the resource.

When to revisit

Revisit this implementation quarterly, after an authentication or Security Rules SDK change, and immediately when you add a role, introduce multi-tenant access, onboard administrators, or investigate an authorization incident. A short review is also warranted when the application begins using Cloud Functions, a custom API, or new Firebase products that need the same permissions.

At each review, compare the role matrix with the deployed claims writer and every enforcement point. Remove obsolete flags, rotate operational access when responsibilities change, and verify that revoked permissions disappear after the client receives a new token. Keep a small regression suite for promotion, downgrade, removal, cross-tenant access, and unauthenticated access.

Practical checklist: define the role matrix; keep claims compact; update claims only from trusted code; force or schedule a controlled token refresh; enforce access in Rules and verified servers; test denials as carefully as approvals; and schedule the next quarterly review. This process prevents stale permissions from becoming a permanent authorization bug.

Related Topics

#Firebase Authentication#Custom Claims#RBAC#Admin SDK#Security Rules
F

Firebase Live Editorial Team

Technical Editor

Senior editor and content strategist. Writing about technology, design, and the future of digital media. Follow along for deep dives into the industry's moving parts.