Skip to content

Commit 0d309f5

Browse files
committed
Fix compilation issues
1 parent 05110e3 commit 0d309f5

File tree

9 files changed

+2140
-186
lines changed

9 files changed

+2140
-186
lines changed

auth/custom_claims.js

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff 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-
11880
function customClaimsServer() {
11981
const app = express();
12082

auth/functions/custom_claims.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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]

0 commit comments

Comments
 (0)