0Pricing
Firebase Auth & Realtime Database Apps · Lesson

Custom Claims & Security Rules

Define custom claims for users and integrate them with Firebase Security Rules to control access to resources.

Custom Claims & Security Rules is a free Firebase Auth & Realtime Database Apps lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Firebase Auth & Realtime Database Apps learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Intro to Custom Claims

Beyond basic user authentication, Firebase allows you to define Custom Claims. These are key-value pairs that you can add to a user's ID token, providing extra information about the user.

Think of them as custom labels or badges attached to a user's identity.

Why Use Custom Claims?

Custom claims are powerful for implementing role-based access control (RBAC) or granting specific permissions within your app. Instead of just knowing 'who' the user is, you can know 'what' they are allowed to do.

  • Designate users as 'admin', 'editor', or 'subscriber'.
  • Grant access to premium features or content.
  • Control data access based on custom attributes.

Setting Claims (Server-Side)

Custom claims are sensitive and must be set by a trusted environment, like your backend server, using the Firebase Admin SDK. This prevents malicious users from giving themselves elevated privileges.

When claims are set, the user's ID token is updated. Clients need to refresh their token to receive the new claims.

Simulating Claim Setting

This conceptual example shows how claims work. On a real backend, the Admin SDK would update a user's profile, and these claims would then be available in their ID token.

import java.util.HashMap;
import java.util.Map;

public class Main {
  public static void main(String[] args) {
    String userId = "user123";
    Map<String, Object> claims = new HashMap<>();

    // --- Server-side action (simulated) ---
    System.out.println("Server sets claims for " + userId);
    claims.put("role", "admin");
    claims.put("level", "premium");

    // --- Client-side action (simulated after token refresh) ---
    System.out.println("\nClient receives ID token with claims:");
    System.out.println("User ID: " + userId);
    System.out.println("Claims: " + claims);

    // Client checks for specific claim
    if (claims.containsKey("role") && claims.get("role").equals("admin")) {
      System.out.println("Access check: User is an admin.");
    } else {
      System.out.println("Access check: Not an admin.");
    }
  }
}

Accessing Claims (Client-Side)

Once a user is logged in and their ID token is refreshed (e.g., after login or explicitly refreshing), your client-side application can read these custom claims from the token.

The claims are embedded within the ID token, which is a JWT (JSON Web Token).

How Claims Power Rules

The true power of custom claims comes when you combine them with Firebase Security Rules. Any custom claim you set on a user's ID token is automatically available within your security rules.

This allows you to create highly specific and dynamic access control logic for your Realtime Database or Cloud Firestore.

Rule Example: Admin Access

Here's how a Firebase Realtime Database Security Rule might use a custom admin: true claim to restrict access to a specific data path.

Only users with this claim in their token would be able to read or write to /adminContent.

{
  "rules": {
    "adminContent": {
      // Only users with 'admin: true' claim can read/write
      ".read": "auth.token.admin === true",
      ".write": "auth.token.admin === true"
    },
    "publicContent": {
      // Anyone authenticated can read, no special claims needed
      ".read": "auth != null",
      ".write": "false"
    }
  }
}

Rule Example: Premium Content

You can also use claims for different levels of access. This rule grants read access to /premiumContent only if the user has a level: 'premium' claim.

This is much more flexible than just checking if a user is logged in.

{
  "rules": {
    "premiumContent": {
      // Only users with 'level: premium' claim can read
      ".read": "auth.token.level === 'premium'",
      ".write": "false"
    },
    "users": {
      "$uid": {
        ".read": "auth.uid === $uid",
        ".write": "auth.uid === $uid"
      }
    }
  }
}

Best Practices for Claims

To ensure efficient and secure use of custom claims:

  • Keep claims small: ID tokens have size limits.
  • Don't store sensitive data: Claims are base64 encoded, not encrypted.
  • Use for authorization: Not for general data storage.
  • Token refresh: Remind users to refresh their ID token if claims change.

Claims Quiz

You've learned how custom claims enhance user roles and security rules. Which statement accurately describes a key aspect of Firebase Custom Claims?

Recap: Custom Claims & Rules

This lesson covered Firebase Custom Claims, a powerful feature for advanced user management.

  • Custom claims allow you to add custom attributes to user ID tokens.
  • They are set securely using the Firebase Admin SDK on your backend.
  • These claims are seamlessly integrated with Firebase Security Rules, enabling robust, role-based access control for your app's resources.
  • Always remember to refresh the client's ID token for changes to take effect.

Frequently asked questions

Is the “Custom Claims & Security Rules” lesson free?

Yes — the full text of “Custom Claims & Security Rules” is free to read here on the web, and the Firebase Auth & Realtime Database Apps course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Firebase Auth & Realtime Database Apps course, upgrade to CoddyKit PRO.

What will I learn in “Custom Claims & Security Rules”?

Define custom claims for users and integrate them with Firebase Security Rules to control access to resources. You practise Firebase Auth & Realtime Database Apps with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Firebase Auth & Realtime Database Apps?

No prior experience is required. Firebase Auth & Realtime Database Apps on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Custom Claims & Security Rules” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Firebase Auth & Realtime Database Apps lesson?

Yes. Every Firebase Auth & Realtime Database Apps lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Phone Number Authentication
  2. Multi-Factor Authentication (MFA)
  3. Custom Claims & Security Rules
  4. Account Linking & Provider Management
← Back to Firebase Auth & Realtime Database Apps