Skip to content

Commit 588cdbb

Browse files
macastelazjhuleatt
andauthored
Add beforeSendEmail and beforeSendSms blocking functions samples (#1253)
--------- Co-authored-by: Jeff <3759507+jhuleatt@users.noreply.github.com>
1 parent a6ae4cb commit 588cdbb

File tree

1 file changed

+64
-3
lines changed
  • Node/quickstarts/auth-blocking-functions/functions

1 file changed

+64
-3
lines changed

Node/quickstarts/auth-blocking-functions/functions/index.js

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,20 @@
1717
const {
1818
beforeUserCreated,
1919
beforeUserSignedIn,
20+
beforeEmailSent,
21+
beforeSmsSent,
2022
HttpsError,
2123
} = require("firebase-functions/identity");
22-
const {admin} = require("firebase-admin");
24+
const {defineString} = require("firebase-functions/params");
25+
const admin = require("firebase-admin");
2326

2427
admin.initializeApp();
2528
const db = admin.firestore();
2629

2730
// [START v2ValidateNewUser]
2831
// [START v2beforeCreateFunctionTrigger]
2932
// Block account creation with any non-acme email address.
30-
exports.validatenewuser = beforeUserCreated((event) => {
33+
exports.validateNewUser = beforeUserCreated((event) => {
3134
// [END v2beforeCreateFunctionTrigger]
3235
// [START v2readUserData]
3336
// User data passed in from the CloudEvent.
@@ -47,7 +50,7 @@ exports.validatenewuser = beforeUserCreated((event) => {
4750
// [START v2CheckForBan]
4851
// [START v2beforeSignInFunctionTrigger]
4952
// Block account sign in with any banned account.
50-
exports.checkforban = beforeUserSignedIn(async (event) => {
53+
exports.checkForBan = beforeUserSignedIn(async (event) => {
5154
// [END v2beforeSignInFunctionTrigger]
5255
// [START v2readEmailData]
5356
// Email passed from the CloudEvent.
@@ -68,3 +71,61 @@ exports.checkforban = beforeUserSignedIn(async (event) => {
6871
// [END v2bannedHttpsError]
6972
});
7073
// [START v2CheckForBan]
74+
75+
// [START v2CheckEmailDomain]
76+
// [START v2beforeEmailSentFunctionTrigger]
77+
// Block email sending with any non-acme email address.
78+
exports.checkEmailDomain = beforeEmailSent((event) => {
79+
// [END v2beforeEmailSentFunctionTrigger]
80+
// [START v2readEmailUser]
81+
// Email passed in from the CloudEvent.
82+
const email = event.data?.email || event.additionalUserInfo?.email;
83+
// [END v2readEmailUser]
84+
85+
// [START v2emailHttpsError]
86+
// Only users of a specific domain can receive emails.
87+
if (!email) {
88+
// Throw an HttpsError so that Firebase Auth rejects the email sending.
89+
throw new HttpsError("invalid-argument",
90+
"No email was found in the CloudEvent");
91+
}
92+
if (!email.endsWith("@acme.com")) {
93+
throw new HttpsError("permission-denied",
94+
"Only users from the acme.com domain can " +
95+
"authenticate");
96+
}
97+
// [END v2emailHttpsError]
98+
});
99+
// [END v2CheckEmailDomain]
100+
101+
// [START v2CheckPhoneNumber]
102+
// [START v2beforeSmsSentFunctionTrigger]
103+
104+
const intlPrefixNumber = defineString("INTERNATIONAL_PREFIX_NUMBER", {
105+
default: "+1",
106+
description: "The country code that we restrict sending to.",
107+
});
108+
// Block SMS sending with any non-US phone number.
109+
exports.checkPhoneNumber = beforeSmsSent((event) => {
110+
// [END v2beforeSmsSentFunctionTrigger]
111+
// [START v2readSmsUser]
112+
// Phone number passed from the CloudEvent.
113+
const phoneNumber = event.data?.phoneNumber ||
114+
event.additionalUserInfo?.phoneNumber;
115+
// [END v2readSmsUser]
116+
117+
// [START v2smsHttpsError]
118+
if (!phoneNumber) {
119+
// Throw an HttpsError so that Firebase Auth rejects the SMS sending.
120+
throw new HttpsError("invalid-argument",
121+
"No phone number was found in the CloudEvent");
122+
}
123+
124+
// Only users of a specific region can receive SMS.
125+
if (!phoneNumber.startsWith(intlPrefixNumber.value())) {
126+
// Throw an HttpsError so that Firebase Auth rejects the SMS sending.
127+
throw new HttpsError("invalid-argument", "Unauthorized phone number");
128+
}
129+
// [END v2smsHttpsError]
130+
});
131+
// [END v2CheckPhoneNumber]

0 commit comments

Comments
 (0)