Skip to content

Commit e3cca75

Browse files
authored
Update to modular v10 SDK (#268)
1 parent e1be313 commit e3cca75

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+2758
-944
lines changed

.github/workflows/node.yml

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ jobs:
1313
strategy:
1414
matrix:
1515
node-version:
16-
- 10.x
1716
- 12.x
1817
steps:
1918
- uses: actions/checkout@v1

auth/create_custom_tokens.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
'use strict';
2-
const admin = require('firebase-admin');
2+
const { initializeApp } = require('firebase-admin/app');
3+
const { getAuth } = require('firebase-admin/auth');
34

45
// Initialize the Admin app with the default appication credentials
56
// [START initialize_sdk_with_default_config]
6-
admin.initializeApp();
7+
initializeApp();
78
// [END initialize_sdk_with_default_config]
89

910
// Initialize the Admin app by providing a service accoune key
1011
// [START initialize_sdk_with_service_account_id]
11-
admin.initializeApp({
12+
initializeApp({
1213
serviceAccountId: '[email protected]',
1314
});
1415
// [END initialize_sdk_with_service_account_id]
1516

1617
// [START custom_token]
1718
const uid = 'some-uid';
1819

19-
admin
20-
.auth()
20+
getAuth()
2121
.createCustomToken(uid)
2222
.then((customToken) => {
2323
// Send token back to client
@@ -33,8 +33,7 @@ const additionalClaims = {
3333
premiumAccount: true,
3434
};
3535

36-
admin
37-
.auth()
36+
getAuth()
3837
.createCustomToken(userId, additionalClaims)
3938
.then((customToken) => {
4039
// Send token back to client

auth/custom_claims.js

+14-55
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
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

79
const uid = 'firebaseUserId123';
810
const 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
@@ -21,8 +22,7 @@ admin
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) {
@@ -33,8 +33,7 @@ admin
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('[email protected]')
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
}
@@ -62,8 +60,7 @@ admin
6260
// [END set_custom_user_claims_script]
6361

6462
// [START set_custom_user_claims_incremental]
65-
admin
66-
.auth()
63+
getAuth()
6764
.getUserByEmail('[email protected]')
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-
12180
function 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

auth/email_action_links.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
2-
const admin = require('firebase-admin');
3-
admin.initializeApp();
2+
const { initializeApp } = require('firebase-admin/app');
3+
const { getAuth } = require('firebase-admin/auth');
4+
initializeApp();
45

56
// [START init_action_code_settings]
67
const actionCodeSettings = {
@@ -25,8 +26,7 @@ const actionCodeSettings = {
2526
// [START password_reset_link]
2627
// Admin SDK API to generate the password reset link.
2728
const userEmail = '[email protected]';
28-
admin
29-
.auth()
29+
getAuth()
3030
.generatePasswordResetLink(userEmail, actionCodeSettings)
3131
.then((link) => {
3232
// Construct password reset email template, embed the link and send
@@ -41,8 +41,7 @@ admin
4141
// [START email_verification_link]
4242
// Admin SDK API to generate the email verification link.
4343
const useremail = '[email protected]';
44-
admin
45-
.auth()
44+
getAuth()
4645
.generateEmailVerificationLink(useremail, actionCodeSettings)
4746
.then((link) => {
4847
// Construct email verification template, embed the link and send
@@ -57,8 +56,7 @@ admin
5756
// [START sign_in_with_email_link]
5857
// Admin SDK API to generate the sign in with email link.
5958
const usremail = '[email protected]';
60-
admin
61-
.auth()
59+
getAuth()
6260
.generateSignInWithEmailLink(usremail, actionCodeSettings)
6361
.then((link) => {
6462
// Construct sign-in with email link template, embed the link and

auth/functions/custom_claims.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// [START auth_custom_claims_cloud_function]
2+
const functions = require('firebase-functions');
3+
const { initializeApp } = require('firebase-admin/app');
4+
const { getAuth } = require('firebase-admin/auth');
5+
const { getDatabase } = require('firebase-admin/database');
6+
7+
initializeApp();
8+
9+
// On sign up.
10+
exports.processSignUp = functions.auth.user().onCreate(async (user) => {
11+
// Check if user meets role criteria.
12+
if (
13+
user.email &&
14+
user.email.endsWith('@admin.example.com') &&
15+
user.emailVerified
16+
) {
17+
const customClaims = {
18+
admin: true,
19+
accessLevel: 9
20+
};
21+
22+
try {
23+
// Set custom user claims on this newly created user.
24+
await getAuth().setCustomUserClaims(user.uid, customClaims);
25+
26+
// Update real-time database to notify client to force refresh.
27+
const metadataRef = getDatabase().ref('metadata/' + user.uid);
28+
29+
// Set the refresh time to the current UTC timestamp.
30+
// This will be captured on the client to force a token refresh.
31+
await metadataRef.set({refreshTime: new Date().getTime()});
32+
} catch (error) {
33+
console.log(error);
34+
}
35+
}
36+
});
37+
// [END auth_custom_claims_cloud_function]

0 commit comments

Comments
 (0)