File tree Expand file tree Collapse file tree 9 files changed +2140
-186
lines changed
Expand file tree Collapse file tree 9 files changed +2140
-186
lines changed Original file line number Diff line number Diff line change @@ -77,44 +77,6 @@ getAuth()
7777 } ) ;
7878// [END set_custom_user_claims_incremental]
7979
80- function customClaimsCloudFunction ( ) {
81- // [START auth_custom_claims_cloud_function]
82- const functions = require ( 'firebase-functions' ) ;
83-
84- const { initializeApp } = require ( 'firebase-admin/app' ) ;
85- initializeApp ( ) ;
86-
87- // On sign up.
88- exports . processSignUp = functions . auth . user ( ) . onCreate ( async ( user ) => {
89- // Check if user meets role criteria.
90- if (
91- user . email &&
92- user . email . endsWith ( '@admin.example.com' ) &&
93- user . emailVerified
94- ) {
95- const customClaims = {
96- admin : true ,
97- accessLevel : 9
98- } ;
99-
100- try {
101- // Set custom user claims on this newly created user.
102- await getAuth ( ) . setCustomUserClaims ( user . uid , customClaims ) ;
103-
104- // Update real-time database to notify client to force refresh.
105- const metadataRef = getDatabase ( ) . ref ( 'metadata/' + user . uid ) ;
106-
107- // Set the refresh time to the current UTC timestamp.
108- // This will be captured on the client to force a token refresh.
109- await metadataRef . set ( { refreshTime : new Date ( ) . getTime ( ) } ) ;
110- } catch ( error ) {
111- console . log ( error ) ;
112- }
113- }
114- } ) ;
115- // [END auth_custom_claims_cloud_function]
116- }
117-
11880function customClaimsServer ( ) {
11981 const app = express ( ) ;
12082
Original file line number Diff line number Diff line change 1+ // [START auth_custom_claims_cloud_function]
2+ const functions = require ( 'firebase-functions' ) ;
3+ const admin = require ( 'firebase-admin' ) ;
4+
5+ admin . initializeApp ( ) ;
6+
7+ // On sign up.
8+ exports . processSignUp = functions . auth . user ( ) . onCreate ( async ( user ) => {
9+ // Check if user meets role criteria.
10+ if (
11+ user . email &&
12+ user . email . endsWith ( '@admin.example.com' ) &&
13+ user . emailVerified
14+ ) {
15+ const customClaims = {
16+ admin : true ,
17+ accessLevel : 9
18+ } ;
19+
20+ try {
21+ // Set custom user claims on this newly created user.
22+ await admin . auth ( ) . setCustomUserClaims ( user . uid , customClaims ) ;
23+
24+ // Update real-time database to notify client to force refresh.
25+ const metadataRef = admin . database ( ) . ref ( 'metadata/' + user . uid ) ;
26+
27+ // Set the refresh time to the current UTC timestamp.
28+ // This will be captured on the client to force a token refresh.
29+ await metadataRef . set ( { refreshTime : new Date ( ) . getTime ( ) } ) ;
30+ } catch ( error ) {
31+ console . log ( error ) ;
32+ }
33+ }
34+ } ) ;
35+ // [END auth_custom_claims_cloud_function]
You can’t perform that action at this time.
0 commit comments