Skip to content

Commit 26759b6

Browse files
committed
Adding snippets for additional Admin Auth guide pages
Adding snippets for custom-claims, email-action-links, and import-users Prior to now, snippets have been hard-coded in the guides.
1 parent f88af1a commit 26759b6

File tree

3 files changed

+279
-0
lines changed

3 files changed

+279
-0
lines changed

auth/custom_claims.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
const admin = require('firebase-admin');
3+
admin.initializeApp();
4+
5+
// [START set_custom_user_claims]
6+
// Set admin privilege on the user corresponding to uid.
7+
8+
admin.auth().setCustomUserClaims(uid, {admin: true}).then(() => {
9+
// The new custom claims will propagate to the user's ID token the
10+
// next time a new one is issued.
11+
});
12+
// [END set_custom_user_claims]
13+
14+
15+
// [START verify_custom_claims]
16+
// Verify the ID token first.
17+
admin.auth().verifyIdToken(idToken).then((claims) => {
18+
if (claims.admin === true) {
19+
// Allow access to requested admin resource.
20+
}
21+
});
22+
// [END verify_custom_claims]

auth/email_action_links.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
'use strict';
2+
const admin = require('firebase-admin');
3+
admin.initializeApp();
4+
5+
// [START init_action_code_settings]
6+
const actionCodeSettings = {
7+
// URL you want to redirect back to. The domain (www.example.com) for
8+
// this URL must be whitelisted in the Firebase Console.
9+
url: 'https://www.example.com/checkout?cartId=1234',
10+
// This must be true for email link sign-in.
11+
handleCodeInApp: true,
12+
iOS: {
13+
bundleId: 'com.example.ios'
14+
},
15+
android: {
16+
packageName: 'com.example.android',
17+
installApp: true,
18+
minimumVersion: '12'
19+
},
20+
// FDL custom domain.
21+
dynamicLinkDomain: 'coolapp.page.link'
22+
};
23+
// [END init_action_code_settings]
24+
25+
// [START email_verification_link]
26+
// Admin SDK API to generate the password reset link.
27+
const email = '[email protected]';
28+
admin.auth().generatePasswordResetLink(email, actionCodeSettings)
29+
.then((link) => {
30+
// Construct password reset email template, embed the link and send
31+
// using custom SMTP server.
32+
return sendCustomPasswordResetEmail(email, displayName, link);
33+
})
34+
.catch((error) => {
35+
// Some error occurred.
36+
});
37+
38+
// [START email_verification_link]
39+
// Admin SDK API to generate the email verification link.
40+
const email = '[email protected]';
41+
admin.auth().generateEmailVerificationLink(email, actionCodeSettings)
42+
.then((link) => {
43+
// Construct email verification template, embed the link and send
44+
// using custom SMTP server.
45+
return sendCustomVerificationEmail(email, displayName, link);
46+
})
47+
.catch((error) => {
48+
// Some error occurred.
49+
});
50+
// [END email_verification_link]
51+
52+
// [START sign_in_with_email_link]
53+
// Admin SDK API to generate the sign in with email link.
54+
const email = '[email protected]';
55+
admin.auth().generateSignInWithEmailLink(email, actionCodeSettings)
56+
.then((link) => {
57+
// Construct sign-in with email link template, embed the link and
58+
// send using custom SMTP server.
59+
return sendSignInEmail(email, displayName, link);
60+
})
61+
.catch((error) => {
62+
// Some error occurred.
63+
});
64+
// [END sign_in_with_email_link]

auth/import_users.js

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
'use strict';
2+
const admin = require('firebase-admin');
3+
admin.initializeApp();
4+
5+
//[START build_user_list]
6+
// Up to 1000 users can be imported at once.
7+
var userImportRecords = [
8+
{
9+
uid: 'uid1',
10+
11+
passwordHash: Buffer.from('passwordHash1'),
12+
passwordSalt: Buffer.from('salt1')
13+
},
14+
{
15+
uid: 'uid2',
16+
17+
passwordHash: Buffer.from('passwordHash2'),
18+
passwordSalt: Buffer.from('salt2')
19+
},
20+
//...
21+
];
22+
//[END build_user_list]
23+
24+
var userImportOptions = {
25+
hash: {
26+
algorithm: 'HMAC_SHA256',
27+
key: Buffer.from('secretKey')
28+
}
29+
};
30+
31+
// [START import_users]
32+
admin.auth().importUsers(userImportRecords, userImportOptions)
33+
.then(function(userImportResult) {
34+
// The number of successful imports is determined via: userImportResult.successCount.
35+
// The number of failed imports is determined via: userImportResult.failureCount.
36+
// To get the error details.
37+
userImportResult.forEach(function(indexedError) {
38+
// The corresponding user that failed to upload.
39+
console.log(userImportRecords[indexedError.index].uid +' failed to import',
40+
indexedError.error);
41+
});
42+
})
43+
.catch(function(error) {
44+
// Some unrecoverable error occurred that prevented the operation from running.
45+
});
46+
// [END import_users]
47+
48+
// [START import_with_hmac]
49+
admin.auth().importUsers([{
50+
uid: 'some-uid',
51+
52+
// Must be provided in a byte buffer.
53+
passwordHash: Buffer.from('password-hash'),
54+
// Must be provided in a byte buffer.
55+
passwordSalt: Buffer.from('salt')
56+
}], {
57+
hash: {
58+
algorithm: 'HMAC_SHA256',
59+
// Must be provided in a byte buffer.
60+
key: Buffer.from('secret')
61+
}
62+
}).then(function(results) {
63+
results.errors.forEach(function(indexedError) {
64+
console.log('Error importing user ' + indexedError.index);
65+
});
66+
}).catch(function(error) {
67+
console.log('Error importing users:', error);
68+
});
69+
// [END import_with_hmac]
70+
71+
// [START import_with_pbkdf]
72+
admin.auth().importUsers([{
73+
uid: 'some-uid',
74+
75+
// Must be provided in a byte buffer.
76+
passwordHash: Buffer.from('password-hash'),
77+
// Must be provided in a byte buffer.
78+
passwordSalt: Buffer.from('salt')
79+
}], {
80+
hash: {
81+
algorithm: 'PBKDF2_SHA256',
82+
rounds: 100000
83+
}
84+
}).then(function(results) {
85+
results.errors.forEach(function(indexedError) {
86+
console.log('Error importing user ' + indexedError.index);
87+
});
88+
}).catch(function(error) {
89+
console.log('Error importing users:', error);
90+
});
91+
// [END import_with_pbkdf]
92+
93+
// [START import_with_standard_scrypt]
94+
admin.auth().importUsers([{
95+
uid: 'some-uid',
96+
97+
// Must be provided in a byte buffer.
98+
passwordHash: Buffer.from('password-hash'),
99+
// Must be provided in a byte buffer.
100+
passwordSalt: Buffer.from('salt')
101+
}], {
102+
hash: {
103+
algorithm: 'STANDARD_SCRYPT',
104+
memoryCost: 1024,
105+
parallelization: 16,
106+
blockSize: 8,
107+
derivedKeyLength: 64
108+
}
109+
}).then(function(results) {
110+
results.errors.forEach(function(indexedError) {
111+
console.log('Error importing user ' + indexedError.index);
112+
});
113+
}).catch(function(error) {
114+
console.log('Error importing users:', error);
115+
});
116+
// [END import_with_standard_scrypt]
117+
118+
// [START import_with_bcrypt]
119+
admin.auth().importUsers([{
120+
uid: 'some-uid',
121+
122+
// Must be provided in a byte buffer.
123+
passwordHash: Buffer.from('password-hash')
124+
}], {
125+
hash: {
126+
algorithm: 'BCRYPT'
127+
}
128+
}).then(function(results) {
129+
results.errors.forEach(function(indexedError) {
130+
console.log('Error importing user ' + indexedError.index);
131+
});
132+
}).catch(function(error) {
133+
console.log('Error importing users:', error);
134+
});
135+
// [END import_with_bcrypt]
136+
137+
138+
// [START import_with_scrypt]
139+
admin.auth().importUsers([{
140+
uid: 'some-uid',
141+
142+
// Must be provided in a byte buffer.
143+
passwordHash: Buffer.from('base64-password-hash', 'base64'),
144+
// Must be provided in a byte buffer.
145+
passwordSalt: Buffer.from('base64-salt', 'base64')
146+
}], {
147+
hash: {
148+
algorithm: 'SCRYPT',
149+
// All the parameters below can be obtained from the Firebase Console's users section.
150+
// Must be provided in a byte buffer.
151+
key: Buffer.from('base64-secret', 'base64'),
152+
saltSeparator: Buffer.from('base64SaltSeparator', 'base64'),
153+
rounds: 8,
154+
memoryCost: 14
155+
}
156+
}).then(function(results) {
157+
results.errors.forEach(function(indexedError) {
158+
console.log('Error importing user ' + indexedError.index);
159+
});
160+
}).catch(function(error) {
161+
console.log('Error importing users:', error);
162+
});
163+
// [END import_with_scrypt]
164+
165+
// [START import_without_password]
166+
admin.auth().importUsers([{
167+
uid: 'some-uid',
168+
displayName: 'John Doe',
169+
170+
photoURL: 'http://www.example.com/12345678/photo.png',
171+
emailVerified: true,
172+
phoneNumber: '+11234567890',
173+
// Set this user as admin.
174+
customClaims: {admin: true},
175+
// User with Google provider.
176+
providerData: [{
177+
uid: 'google-uid',
178+
179+
displayName: 'John Doe',
180+
photoURL: 'http://www.example.com/12345678/photo.png',
181+
providerId: 'google.com'
182+
}]
183+
}]).then(function(results) {
184+
results.errors.forEach(function(indexedError) {
185+
console.log('Error importing user ' + indexedError.index);
186+
});
187+
}).catch(function(error) {
188+
console.log('Error importing users:', error);
189+
});
190+
// [END import_without_password]
191+
192+
193+

0 commit comments

Comments
 (0)