Skip to content

Commit 027ef5b

Browse files
committed
Merge branch 'master' into modular-admin
2 parents ecd6fc7 + 56cfbf8 commit 027ef5b

37 files changed

+9758
-1978
lines changed

auth/custom_claims.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
'use strict';
22
const { initializeApp } = require('firebase-admin/app');
33
const { getAuth } = require('firebase-admin/auth');
4+
const { getDatabase } = require('firebase-admin/database');
45
initializeApp();
56

7+
const express = require('express');
8+
69
const uid = 'firebaseUserId123';
710
const idToken = 'some-invalid-token';
811

@@ -73,3 +76,76 @@ getAuth()
7376
console.log(error);
7477
});
7578
// [END set_custom_user_claims_incremental]
79+
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+
118+
function customClaimsServer() {
119+
const app = express();
120+
121+
// [START auth_custom_claims_server]
122+
app.post('/setCustomClaims', async (req, res) => {
123+
// Get the ID token passed.
124+
const idToken = req.body.idToken;
125+
126+
// Verify the ID token and decode its payload.
127+
const claims = await getAuth().verifyIdToken(idToken);
128+
129+
// Verify user is eligible for additional privileges.
130+
if (
131+
typeof claims.email !== 'undefined' &&
132+
typeof claims.email_verified !== 'undefined' &&
133+
claims.email_verified &&
134+
claims.email.endsWith('@admin.example.com')
135+
) {
136+
// Add custom claims for additional privileges.
137+
await getAuth().setCustomUserClaims(claims.sub, {
138+
admin: true
139+
});
140+
141+
// Tell client to refresh token on user.
142+
res.end(JSON.stringify({
143+
status: 'success'
144+
}));
145+
} else {
146+
// Return nothing.
147+
res.end(JSON.stringify({ status: 'ineligible' }));
148+
}
149+
});
150+
// [END auth_custom_claims_server]
151+
}

0 commit comments

Comments
 (0)