Skip to content

Commit 4c058ea

Browse files
authored
Add Auth Blocking samples for v1 & v2 functions (#1003)
1 parent 7209bd3 commit 4c058ea

File tree

14 files changed

+410
-0
lines changed

14 files changed

+410
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
firebase-debug.log*
8+
firebase-debug.*.log*
9+
10+
# Firebase cache
11+
.firebase/
12+
13+
# Firebase config
14+
15+
# Uncomment this if you'd like others to create their own Firebase project.
16+
# For a team working on the same Firebase project(s), it is recommended to leave
17+
# it commented so all members can deploy to the same project(s) in .firebaserc.
18+
# .firebaserc
19+
20+
# Runtime data
21+
pids
22+
*.pid
23+
*.seed
24+
*.pid.lock
25+
26+
# Directory for instrumented libs generated by jscoverage/JSCover
27+
lib-cov
28+
29+
# Coverage directory used by tools like istanbul
30+
coverage
31+
32+
# nyc test coverage
33+
.nyc_output
34+
35+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
36+
.grunt
37+
38+
# Bower dependency directory (https://bower.io/)
39+
bower_components
40+
41+
# node-waf configuration
42+
.lock-wscript
43+
44+
# Compiled binary addons (http://nodejs.org/api/addons.html)
45+
build/Release
46+
47+
# Dependency directories
48+
node_modules/
49+
50+
# Optional npm cache directory
51+
.npm
52+
53+
# Optional eslint cache
54+
.eslintcache
55+
56+
# Optional REPL history
57+
.node_repl_history
58+
59+
# Output of 'npm pack'
60+
*.tgz
61+
62+
# Yarn Integrity file
63+
.yarn-integrity
64+
65+
# dotenv environment variables file
66+
.env
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Firebase SDK for Cloud Functions 2nd Gen Quickstart - Auth Blocking Functions
2+
================================================
3+
4+
The Auth Blocking functions Quickstart demonstrates how to block account sign in and creation when using Firebase Auth or Google Cloud Identity Platform in a Firebase App.
5+
6+
7+
- [Read more about auth blocking functions](https://firebase.google.com/docs/auth/extend-with-blocking-functions)
8+
- [Read more about Cloud Functions for Firebase](https://firebase.google.com/docs/functions/)
9+
10+
11+
Getting Started
12+
---------------
13+
14+
To try this sample, you need a test app with Firebase Auth and Cloud Firestore enabled. Don't use a live app with real users!
15+
16+
1. Install dependencies with `npm install`
17+
2. Deploy the functions with `firebase deploy --only functions`
18+
3. Try to create an account using an email address with a domain _other than_ `@acme.com`. It should fail.
19+
4. Add an existing user's email address to the `banned` collection in Cloud Firestore. Then, try to sign in as that user. It should fail.
20+
21+
22+
License
23+
-------
24+
25+
© Google, 2022. Licensed under an [Apache-2](../../../LICENSE) license.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"functions": {
3+
"predeploy": [
4+
"npm --prefix \"$RESOURCE_DIR\" run lint"
5+
]
6+
}
7+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports = {
2+
root: true,
3+
env: {
4+
es6: true,
5+
node: true,
6+
},
7+
extends: [
8+
"eslint:recommended",
9+
"google",
10+
],
11+
rules: {
12+
quotes: ["error", "double"],
13+
},
14+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
const {beforeUserCreated, beforeUserSignedIn, HttpsError} = require("firebase-functions/v2/identity");
18+
const {admin} = require("firebase-admin");
19+
20+
admin.initializeApp();
21+
const db = admin.firestore();
22+
23+
// [START v2ValidateNewUser]
24+
// [START v2beforeCreateFunctionTrigger]
25+
// Block account creation with any non-acme email address.
26+
exports.validatenewuser = beforeUserCreated((event) => {
27+
// [END v2beforeCreateFunctionTrigger]
28+
// [START v2readUserData]
29+
// User data passed in from the CloudEvent.
30+
const user = event.data;
31+
// [END v2readUserData]
32+
33+
// [START v2domainHttpsError]
34+
// Only users of a specific domain can sign up.
35+
if (!user?.email?.includes('@acme.com')) {
36+
// Throwing an HttpsError so that the Auth service rejects the account creation.
37+
throw new HttpsError('invalid-argument', "Unauthorized email");
38+
}
39+
// [END v2domainHttpsError]
40+
});
41+
// [END v2ValidateNewUser]
42+
43+
// [START v2CheckForBan]
44+
// [START v2beforeSignInFunctionTrigger]
45+
// Block account sign in with any banned account.
46+
exports.checkforban = beforeUserSignedIn(async (event) => {
47+
// [END v2beforeSignInFunctionTrigger]
48+
// [START v2readEmailData]
49+
// Email passed from the CloudEvent.
50+
const email = event.data.email || "";
51+
// [END v2readEmailData]
52+
53+
// [START v2documentGet]
54+
// Obtain a document in Firestore of the banned email address.
55+
const doc = await db.collection("banned").doc(email).get();
56+
// [END v2documentGet]
57+
58+
// [START v2bannedHttpsError]
59+
// Checking that the document exists for the email address.
60+
if (doc.exists) {
61+
// Throwing an HttpsError so that the Auth service rejects the account sign in.
62+
throw new HttpsError('invalid-argument', "Unauthorized email");
63+
}
64+
// [END v2bannedHttpsError]
65+
});
66+
// [START v2CheckForBan]
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "functions",
3+
"description": "Cloud Functions for Firebase",
4+
"scripts": {
5+
"lint": "eslint .",
6+
"serve": "firebase emulators:start --only functions",
7+
"shell": "firebase functions:shell",
8+
"start": "npm run shell",
9+
"deploy": "firebase deploy --only functions",
10+
"logs": "firebase functions:log"
11+
},
12+
"engines": {
13+
"node": "16"
14+
},
15+
"main": "index.js",
16+
"dependencies": {
17+
"firebase-admin": "^10.1.0",
18+
"firebase-functions": "^3.22.0"
19+
},
20+
"devDependencies": {
21+
"eslint": "^8.9.0",
22+
"eslint-config-google": "^0.14.0",
23+
"firebase-functions-test": "^0.2.0"
24+
},
25+
"private": true
26+
}

auth-blocking-functions/.gitignore

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
firebase-debug.log*
8+
firebase-debug.*.log*
9+
10+
# Firebase cache
11+
.firebase/
12+
13+
# Firebase config
14+
15+
# Uncomment this if you'd like others to create their own Firebase project.
16+
# For a team working on the same Firebase project(s), it is recommended to leave
17+
# it commented so all members can deploy to the same project(s) in .firebaserc.
18+
# .firebaserc
19+
20+
# Runtime data
21+
pids
22+
*.pid
23+
*.seed
24+
*.pid.lock
25+
26+
# Directory for instrumented libs generated by jscoverage/JSCover
27+
lib-cov
28+
29+
# Coverage directory used by tools like istanbul
30+
coverage
31+
32+
# nyc test coverage
33+
.nyc_output
34+
35+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
36+
.grunt
37+
38+
# Bower dependency directory (https://bower.io/)
39+
bower_components
40+
41+
# node-waf configuration
42+
.lock-wscript
43+
44+
# Compiled binary addons (http://nodejs.org/api/addons.html)
45+
build/Release
46+
47+
# Dependency directories
48+
node_modules/
49+
50+
# Optional npm cache directory
51+
.npm
52+
53+
# Optional eslint cache
54+
.eslintcache
55+
56+
# Optional REPL history
57+
.node_repl_history
58+
59+
# Output of 'npm pack'
60+
*.tgz
61+
62+
# Yarn Integrity file
63+
.yarn-integrity
64+
65+
# dotenv environment variables file
66+
.env

auth-blocking-functions/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Firebase SDK for Cloud Functions Quickstart - Auth Blocking Functions
2+
================================================
3+
4+
The Auth Blocking functions Quickstart demonstrates how to block account sign in and creation when using Firebase Auth or Google Cloud Identity Platform in a Firebase App.
5+
6+
7+
- [Read more about auth blocking functions](https://firebase.google.com/docs/auth/extend-with-blocking-functions)
8+
- [Read more about Cloud Functions for Firebase](https://firebase.google.com/docs/functions/)
9+
10+
11+
Getting Started
12+
---------------
13+
14+
To try this sample, you need a test app with Firebase Auth and Cloud Firestore enabled. Don't use a live app with real users!
15+
16+
1. Install dependencies with `npm install`
17+
2. Deploy the functions with `firebase deploy --only functions`
18+
3. Try to create an account using an email address with a domain _other than_ `@acme.com`. It should fail.
19+
4. Add an existing user's email address to the `banned` collection in Cloud Firestore. Then, try to sign in as that user. It should fail.
20+
21+
22+
License
23+
-------
24+
25+
© Google, 2022. Licensed under an [Apache-2](../../../LICENSE) license.

auth-blocking-functions/firebase.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"functions": {
3+
"predeploy": [
4+
"npm --prefix \"$RESOURCE_DIR\" run lint"
5+
]
6+
}
7+
}

0 commit comments

Comments
 (0)