Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
762c22a
Update to exp version
samtstern Apr 16, 2021
eba953d
Update auth snippets
samtstern Apr 16, 2021
39cda2c
Remote config
samtstern Apr 16, 2021
e95db7c
FirebaseApp
samtstern Apr 16, 2021
ec05596
Firestore
samtstern Apr 16, 2021
f65c6f4
Management
samtstern Apr 16, 2021
9ae0a11
Move to const/require
samtstern Apr 19, 2021
02e329b
Fix import
samtstern Apr 19, 2021
ecd6fc7
Drop Node 10 support
samtstern Apr 19, 2021
138f4b3
Merge branch 'master' into modular-admin
samtstern Apr 21, 2021
027ef5b
Merge branch 'master' into modular-admin
samtstern May 13, 2021
1417029
Merge branch 'modular-admin' of github.com:firebase/snippets-node int…
samtstern May 13, 2021
05110e3
Downgrade SDK where functions is used
samtstern May 13, 2021
0d309f5
Fix compilation issues
samtstern May 14, 2021
3829184
Merge branch 'master' into HEAD
samtstern May 26, 2021
eecfc0b
Bootstrap
samtstern May 26, 2021
d59c18a
functions-stackdriver-logging
samtstern May 26, 2021
396cdfd
firestore-extend-with-functions
samtstern May 26, 2021
37da9bf
firestore-solution-aggregation
samtstern May 26, 2021
000c072
auth-functions
samtstern May 26, 2021
2c67448
firestore-export
samtstern May 26, 2021
472161e
firestore-solution-deletes
samtstern May 26, 2021
c15bc84
exp --> 9.100.0-alpha.0
samtstern May 26, 2021
0de2774
v10
samtstern Oct 15, 2021
47185fb
Merge branch 'master' into modular-admin
samtstern Oct 15, 2021
81c4b61
v10 again
samtstern Oct 15, 2021
b172640
Fix lockfiles
samtstern Oct 15, 2021
dcb9bd0
Cleanup some merge issues
samtstern Oct 15, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/node.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ jobs:
strategy:
matrix:
node-version:
- 10.x
- 12.x
steps:
- uses: actions/checkout@v1
Expand Down
13 changes: 6 additions & 7 deletions auth/create_custom_tokens.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
'use strict';
const admin = require('firebase-admin');
const { initializeApp } = require('firebase-admin/app');
const { getAuth } = require('firebase-admin/auth');

// Initialize the Admin app with the default appication credentials
// [START initialize_sdk_with_default_config]
admin.initializeApp();
initializeApp();
// [END initialize_sdk_with_default_config]

// Initialize the Admin app by providing a service accoune key
// [START initialize_sdk_with_service_account_id]
admin.initializeApp({
initializeApp({
serviceAccountId: '[email protected]',
});
// [END initialize_sdk_with_service_account_id]

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

admin
.auth()
getAuth()
.createCustomToken(uid)
.then((customToken) => {
// Send token back to client
Expand All @@ -33,8 +33,7 @@ const additionalClaims = {
premiumAccount: true,
};

admin
.auth()
getAuth()
.createCustomToken(userId, additionalClaims)
.then((customToken) => {
// Send token back to client
Expand Down
69 changes: 14 additions & 55 deletions auth/custom_claims.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
'use strict';
const express = require('express');
const { initializeApp } = require('firebase-admin/app');
const { getAuth } = require('firebase-admin/auth');
const { getDatabase } = require('firebase-admin/database');
initializeApp();

const admin = require('firebase-admin');
admin.initializeApp();
const express = require('express');

const uid = 'firebaseUserId123';
const idToken = 'some-invalid-token';

// [START set_custom_user_claims]
// Set admin privilege on the user corresponding to uid.

admin
.auth()
getAuth()
.setCustomUserClaims(uid, { admin: true })
.then(() => {
// The new custom claims will propagate to the user's ID token the
Expand All @@ -21,8 +22,7 @@ admin

// [START verify_custom_claims]
// Verify the ID token first.
admin
.auth()
getAuth()
.verifyIdToken(idToken)
.then((claims) => {
if (claims.admin === true) {
Expand All @@ -33,8 +33,7 @@ admin

// [START read_custom_user_claims]
// Lookup the user associated with the specified uid.
admin
.auth()
getAuth()
.getUser(uid)
.then((userRecord) => {
// The claims can be accessed on the user record.
Expand All @@ -43,15 +42,14 @@ admin
// [END read_custom_user_claims]

// [START set_custom_user_claims_script]
admin
.auth()
getAuth()
.getUserByEmail('[email protected]')
.then((user) => {
// Confirm user is verified.
if (user.emailVerified) {
// Add custom claims for additional privileges.
// This will be picked up by the user on token refresh or next sign in on new device.
return admin.auth().setCustomUserClaims(user.uid, {
return getAuth().setCustomUserClaims(user.uid, {
admin: true,
});
}
Expand All @@ -62,8 +60,7 @@ admin
// [END set_custom_user_claims_script]

// [START set_custom_user_claims_incremental]
admin
.auth()
getAuth()
.getUserByEmail('[email protected]')
.then((user) => {
// Add incremental custom claim without overwriting existing claims.
Expand All @@ -72,52 +69,14 @@ admin
// Add level.
currentCustomClaims['accessLevel'] = 10;
// Add custom claims for additional privileges.
return admin.auth().setCustomUserClaims(user.uid, currentCustomClaims);
return getAuth().setCustomUserClaims(user.uid, currentCustomClaims);
}
})
.catch((error) => {
console.log(error);
});
// [END set_custom_user_claims_incremental]

function customClaimsCloudFunction() {
// [START auth_custom_claims_cloud_function]
const functions = require('firebase-functions');

const admin = require('firebase-admin');
admin.initializeApp();

// On sign up.
exports.processSignUp = functions.auth.user().onCreate(async (user) => {
// Check if user meets role criteria.
if (
user.email &&
user.email.endsWith('@admin.example.com') &&
user.emailVerified
) {
const customClaims = {
admin: true,
accessLevel: 9
};

try {
// Set custom user claims on this newly created user.
await admin.auth().setCustomUserClaims(user.uid, customClaims);

// Update real-time database to notify client to force refresh.
const metadataRef = admin.database().ref('metadata/' + user.uid);

// Set the refresh time to the current UTC timestamp.
// This will be captured on the client to force a token refresh.
await metadataRef.set({refreshTime: new Date().getTime()});
} catch (error) {
console.log(error);
}
}
});
// [END auth_custom_claims_cloud_function]
}

function customClaimsServer() {
const app = express();

Expand All @@ -127,7 +86,7 @@ function customClaimsServer() {
const idToken = req.body.idToken;

// Verify the ID token and decode its payload.
const claims = await admin.auth().verifyIdToken(idToken);
const claims = await getAuth().verifyIdToken(idToken);

// Verify user is eligible for additional privileges.
if (
Expand All @@ -137,7 +96,7 @@ function customClaimsServer() {
claims.email.endsWith('@admin.example.com')
) {
// Add custom claims for additional privileges.
await admin.auth().setCustomUserClaims(claims.sub, {
await getAuth().setCustomUserClaims(claims.sub, {
admin: true
});

Expand Down
14 changes: 6 additions & 8 deletions auth/email_action_links.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
const admin = require('firebase-admin');
admin.initializeApp();
const { initializeApp } = require('firebase-admin/app');
const { getAuth } = require('firebase-admin/auth');
initializeApp();

// [START init_action_code_settings]
const actionCodeSettings = {
Expand All @@ -25,8 +26,7 @@ const actionCodeSettings = {
// [START password_reset_link]
// Admin SDK API to generate the password reset link.
const userEmail = '[email protected]';
admin
.auth()
getAuth()
.generatePasswordResetLink(userEmail, actionCodeSettings)
.then((link) => {
// Construct password reset email template, embed the link and send
Expand All @@ -41,8 +41,7 @@ admin
// [START email_verification_link]
// Admin SDK API to generate the email verification link.
const useremail = '[email protected]';
admin
.auth()
getAuth()
.generateEmailVerificationLink(useremail, actionCodeSettings)
.then((link) => {
// Construct email verification template, embed the link and send
Expand All @@ -57,8 +56,7 @@ admin
// [START sign_in_with_email_link]
// Admin SDK API to generate the sign in with email link.
const usremail = '[email protected]';
admin
.auth()
getAuth()
.generateSignInWithEmailLink(usremail, actionCodeSettings)
.then((link) => {
// Construct sign-in with email link template, embed the link and
Expand Down
37 changes: 37 additions & 0 deletions auth/functions/custom_claims.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// [START auth_custom_claims_cloud_function]
const functions = require('firebase-functions');
const { initializeApp } = require('firebase-admin/app');
const { getAuth } = require('firebase-admin/auth');
const { getDatabase } = require('firebase-admin/database');

initializeApp();

// On sign up.
exports.processSignUp = functions.auth.user().onCreate(async (user) => {
// Check if user meets role criteria.
if (
user.email &&
user.email.endsWith('@admin.example.com') &&
user.emailVerified
) {
const customClaims = {
admin: true,
accessLevel: 9
};

try {
// Set custom user claims on this newly created user.
await getAuth().setCustomUserClaims(user.uid, customClaims);

// Update real-time database to notify client to force refresh.
const metadataRef = getDatabase().ref('metadata/' + user.uid);

// Set the refresh time to the current UTC timestamp.
// This will be captured on the client to force a token refresh.
await metadataRef.set({refreshTime: new Date().getTime()});
} catch (error) {
console.log(error);
}
}
});
// [END auth_custom_claims_cloud_function]
Loading