Configure custom claims on users

This document explains how to configure custom claims on users with Identity Platform. Custom claims are inserted into user tokens during authentication. Your app can use these claims to handle complex authorization scenarios, such as restricting a user's access to a resource based on their role.

Set up custom claims

To preserve security, set custom claims using the Admin SDK on your server:

  1. If you haven't already, Install the Admin SDK .

  2. Set the custom claim you want to use. In the following example, a custom claim is set on the user to describe that they're an administrator:

    Node.js

     // Set admin privilege on the user corresponding to uid. 
     getAuth 
     () 
      
     . 
     setCustomUserClaims 
     ( 
     uid 
     , 
      
     { 
      
     admin 
     : 
      
     true 
      
     }) 
      
     . 
     then 
     (() 
      
     = 
    >  
     { 
      
     // The new custom claims will propagate to the user's ID token the 
      
     // next time a new one is issued. 
      
     }); 
      
    

    Java

     // Set admin privilege on the user corresponding to uid. 
     Map<String 
     , 
      
     Object 
    >  
     claims 
      
     = 
      
     new 
      
     HashMap 
    <> (); 
     claims 
     . 
     put 
     ( 
     "admin" 
     , 
      
     true 
     ); 
     FirebaseAuth 
     . 
     getInstance 
     (). 
     setCustomUserClaims 
     ( 
     uid 
     , 
      
     claims 
     ); 
     // The new custom claims will propagate to the user's ID token the 
     // next time a new one is issued.  
     
    

Python

 # Set admin privilege on the user corresponding to uid. 
 auth 
 . 
 set_custom_user_claims 
 ( 
 uid 
 , 
 { 
 'admin' 
 : 
 True 
 }) 
 # The new custom claims will propagate to the user's ID token the 
 # next time a new one is issued.  
 

Go

 // Get an auth client from the firebase.App 
 client 
 , 
  
 err 
  
 := 
  
 app 
 . 
 Auth 
 ( 
 ctx 
 ) 
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 log 
 . 
 Fatalf 
 ( 
 "error getting Auth client: %v\n" 
 , 
  
 err 
 ) 
 } 
 // Set admin privilege on the user corresponding to uid. 
 claims 
  
 := 
  
 map 
 [ 
 string 
 ] 
 interface 
 {}{ 
 "admin" 
 : 
  
 true 
 } 
 err 
  
 = 
  
 client 
 . 
 SetCustomUserClaims 
 ( 
 ctx 
 , 
  
 uid 
 , 
  
 claims 
 ) 
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 log 
 . 
 Fatalf 
 ( 
 "error setting custom claims %v\n" 
 , 
  
 err 
 ) 
 } 
 // The new custom claims will propagate to the user's ID token the 
 // next time a new one is issued.  
 

C#

 // Set admin privileges on the user corresponding to uid. 
 var 
  
 claims 
  
 = 
  
 new 
  
 Dictionary<string 
 , 
  
 object 
> () 
 { 
  
 { 
  
 "admin" 
 , 
  
 true 
  
 }, 
 }; 
 await 
  
 FirebaseAuth 
 . 
 DefaultInstance 
 . 
 SetCustomUserClaimsAsync 
 ( 
 uid 
 , 
  
 claims 
 ); 
 // The new custom claims will propagate to the user's ID token the 
 // next time a new one is issued.  
 
  • Validate the custom claim the next time it's sent to your server:

    Node.js

     // Verify the ID token first. 
     getAuth 
     () 
      
     . 
     verifyIdToken 
     ( 
     idToken 
     ) 
      
     . 
     then 
     (( 
     claims 
     ) 
      
     = 
    >  
     { 
      
     if 
      
     ( 
     claims 
     . 
     admin 
      
     === 
      
     true 
     ) 
      
     { 
      
     // Allow access to requested admin resource. 
      
     } 
      
     }); 
      
    

    Java

     // Verify the ID token first. 
     FirebaseToken 
      
     decoded 
      
     = 
      
     FirebaseAuth 
     . 
     getInstance 
     (). 
     verifyIdToken 
     ( 
     idToken 
     ); 
     if 
      
     ( 
     Boolean 
     . 
     TRUE 
     . 
     equals 
     ( 
     decoded 
     . 
     getClaims 
     (). 
     get 
     ( 
     "admin" 
     ))) 
      
     { 
      
     // Allow access to requested admin resource. 
     } 
      
    

    Python

     # Verify the ID token first. 
     claims 
     = 
     auth 
     . 
     verify_id_token 
     ( 
     id_token 
     ) 
     if 
     claims 
     [ 
     'admin' 
     ] 
     is 
     True 
     : 
     # Allow access to requested admin resource. 
     pass  
     
     . 
     py 
    
  • Go

     // Verify the ID token first. 
     token 
     , 
      
     err 
      
     := 
      
     client 
     . 
     VerifyIDToken 
     ( 
     ctx 
     , 
      
     idToken 
     ) 
     if 
      
     err 
      
     != 
      
     nil 
      
     { 
      
     log 
     . 
     Fatal 
     ( 
     err 
     ) 
     } 
     claims 
      
     := 
      
     token 
     . 
     Claims 
     if 
      
     admin 
     , 
      
     ok 
      
     := 
      
     claims 
     [ 
     "admin" 
     ]; 
      
     ok 
      
     { 
      
     if 
      
     admin 
     .( 
     bool 
     ) 
      
     { 
      
     //Allow access to requested admin resource. 
      
     } 
     } 
      
    

    C#

     // Verify the ID token first. 
     FirebaseToken 
      
     decoded 
      
     = 
      
     await 
      
     FirebaseAuth 
     . 
     DefaultInstance 
     . 
     VerifyIdTokenAsync 
     ( 
     idToken 
     ); 
     object 
      
     isAdmin 
     ; 
     if 
      
     ( 
     decoded 
     . 
     Claims 
     . 
     TryGetValue 
     ( 
     "admin" 
     , 
      
     out 
      
     isAdmin 
     )) 
     { 
      
     if 
      
     (( 
     bool 
     ) 
     isAdmin 
     ) 
      
     { 
      
     // Allow access to requested admin resource. 
      
     } 
     } 
      
    
  • To determine what custom claims are present for a user:

    Node.js

     // Lookup the user associated with the specified uid. 
     getAuth 
     () 
      
     . 
     getUser 
     ( 
     uid 
     ) 
      
     . 
     then 
     (( 
     userRecord 
     ) 
      
     = 
    >  
     { 
      
     // The claims can be accessed on the user record. 
      
     console 
     . 
     log 
     ( 
     userRecord 
     . 
     customClaims 
     [ 
     'admin' 
     ]); 
      
     }); 
      
    

    Java

     // Lookup the user associated with the specified uid. 
     UserRecord 
      
     user 
      
     = 
      
     FirebaseAuth 
     . 
     getInstance 
     (). 
     getUser 
     ( 
     uid 
     ); 
     System 
     . 
     out 
     . 
     println 
     ( 
     user 
     . 
     getCustomClaims 
     (). 
     get 
     ( 
     "admin" 
     )); 
      
    

    Python

     # Lookup the user associated with the specified uid. 
     user 
     = 
     auth 
     . 
     get_user 
     ( 
     uid 
     ) 
     # The claims can be accessed on the user record. 
     print 
     ( 
     user 
     . 
     custom_claims 
     . 
     get 
     ( 
     'admin' 
     )) 
      
    

    Go

     // Lookup the user associated with the specified uid. 
     user 
     , 
      
     err 
      
     := 
      
     client 
     . 
     GetUser 
     ( 
     ctx 
     , 
      
     uid 
     ) 
     if 
      
     err 
      
     != 
      
     nil 
      
     { 
      
     log 
     . 
     Fatal 
     ( 
     err 
     ) 
     } 
     // The claims can be accessed on the user record. 
     if 
      
     admin 
     , 
      
     ok 
      
     := 
      
     user 
     . 
     CustomClaims 
     [ 
     "admin" 
     ]; 
      
     ok 
      
     { 
      
     if 
      
     admin 
     .( 
     bool 
     ) 
      
     { 
      
     log 
     . 
     Println 
     ( 
     admin 
     ) 
      
     } 
     } 
      
    

    C#

     // Lookup the user associated with the specified uid. 
     UserRecord 
      
     user 
      
     = 
      
     await 
      
     FirebaseAuth 
     . 
     DefaultInstance 
     . 
     GetUserAsync 
     ( 
     uid 
     ); 
     Console 
     . 
     WriteLine 
     ( 
     user 
     . 
     CustomClaims 
     [ 
     "admin" 
     ]); 
      
    
  • When setting up custom claims, keep the following in mind:

    • Custom claims cannot exceed 1000 bytes in size. Attempting to pass claims larger than 1000 bytes results in an error.
    • Custom claims are inserted into the user JWT when the token is issued. New claims are not available until the token is refreshed. You can refresh a token silently by calling user.getIdToken(true) .
    • To maintain continuity and security, only set custom claims in a secure server environment.

    What's next

    Create a Mobile Website
    View Site in Mobile | Classic
    Share by: