11'use strict' ;
2- const express = require ( 'express' ) ;
2+ const { initializeApp } = require ( 'firebase-admin/app' ) ;
3+ const { getAuth } = require ( 'firebase-admin/auth' ) ;
4+ const { getDatabase } = require ( 'firebase-admin/database' ) ;
5+ initializeApp ( ) ;
36
4- const admin = require ( 'firebase-admin' ) ;
5- admin . initializeApp ( ) ;
7+ const express = require ( 'express' ) ;
68
79const uid = 'firebaseUserId123' ;
810const idToken = 'some-invalid-token' ;
911
1012// [START set_custom_user_claims]
1113// Set admin privilege on the user corresponding to uid.
1214
13- admin
14- . auth ( )
15+ getAuth ( )
1516 . setCustomUserClaims ( uid , { admin : true } )
1617 . then ( ( ) => {
1718 // The new custom claims will propagate to the user's ID token the
2122
2223// [START verify_custom_claims]
2324// Verify the ID token first.
24- admin
25- . auth ( )
25+ getAuth ( )
2626 . verifyIdToken ( idToken )
2727 . then ( ( claims ) => {
2828 if ( claims . admin === true ) {
3333
3434// [START read_custom_user_claims]
3535// Lookup the user associated with the specified uid.
36- admin
37- . auth ( )
36+ getAuth ( )
3837 . getUser ( uid )
3938 . then ( ( userRecord ) => {
4039 // The claims can be accessed on the user record.
@@ -43,15 +42,14 @@ admin
4342// [END read_custom_user_claims]
4443
4544// [START set_custom_user_claims_script]
46- admin
47- . auth ( )
45+ getAuth ( )
4846 . getUserByEmail ( 'user@admin.example.com' )
4947 . then ( ( user ) => {
5048 // Confirm user is verified.
5149 if ( user . emailVerified ) {
5250 // Add custom claims for additional privileges.
5351 // This will be picked up by the user on token refresh or next sign in on new device.
54- return admin . auth ( ) . setCustomUserClaims ( user . uid , {
52+ return getAuth ( ) . setCustomUserClaims ( user . uid , {
5553 admin : true ,
5654 } ) ;
5755 }
6260// [END set_custom_user_claims_script]
6361
6462// [START set_custom_user_claims_incremental]
65- admin
66- . auth ( )
63+ getAuth ( )
6764 . getUserByEmail ( 'user@admin.example.com' )
6865 . then ( ( user ) => {
6966 // Add incremental custom claim without overwriting existing claims.
@@ -72,52 +69,14 @@ admin
7269 // Add level.
7370 currentCustomClaims [ 'accessLevel' ] = 10 ;
7471 // Add custom claims for additional privileges.
75- return admin . auth ( ) . setCustomUserClaims ( user . uid , currentCustomClaims ) ;
72+ return getAuth ( ) . setCustomUserClaims ( user . uid , currentCustomClaims ) ;
7673 }
7774 } )
7875 . catch ( ( error ) => {
7976 console . log ( error ) ;
8077 } ) ;
8178// [END set_custom_user_claims_incremental]
8279
83- function customClaimsCloudFunction ( ) {
84- // [START auth_custom_claims_cloud_function]
85- const functions = require ( 'firebase-functions' ) ;
86-
87- const admin = require ( 'firebase-admin' ) ;
88- admin . initializeApp ( ) ;
89-
90- // On sign up.
91- exports . processSignUp = functions . auth . user ( ) . onCreate ( async ( user ) => {
92- // Check if user meets role criteria.
93- if (
94- user . email &&
95- user . email . endsWith ( '@admin.example.com' ) &&
96- user . emailVerified
97- ) {
98- const customClaims = {
99- admin : true ,
100- accessLevel : 9
101- } ;
102-
103- try {
104- // Set custom user claims on this newly created user.
105- await admin . auth ( ) . setCustomUserClaims ( user . uid , customClaims ) ;
106-
107- // Update real-time database to notify client to force refresh.
108- const metadataRef = admin . database ( ) . ref ( 'metadata/' + user . uid ) ;
109-
110- // Set the refresh time to the current UTC timestamp.
111- // This will be captured on the client to force a token refresh.
112- await metadataRef . set ( { refreshTime : new Date ( ) . getTime ( ) } ) ;
113- } catch ( error ) {
114- console . log ( error ) ;
115- }
116- }
117- } ) ;
118- // [END auth_custom_claims_cloud_function]
119- }
120-
12180function customClaimsServer ( ) {
12281 const app = express ( ) ;
12382
@@ -127,7 +86,7 @@ function customClaimsServer() {
12786 const idToken = req . body . idToken ;
12887
12988 // Verify the ID token and decode its payload.
130- const claims = await admin . auth ( ) . verifyIdToken ( idToken ) ;
89+ const claims = await getAuth ( ) . verifyIdToken ( idToken ) ;
13190
13291 // Verify user is eligible for additional privileges.
13392 if (
@@ -137,7 +96,7 @@ function customClaimsServer() {
13796 claims . email . endsWith ( '@admin.example.com' )
13897 ) {
13998 // Add custom claims for additional privileges.
140- await admin . auth ( ) . setCustomUserClaims ( claims . sub , {
99+ await getAuth ( ) . setCustomUserClaims ( claims . sub , {
141100 admin : true
142101 } ) ;
143102
0 commit comments