Skip to content

Commit b87ceec

Browse files
committed
Add beforeSendEmail and beforeSendSms blocking functions samples
1 parent a6ae4cb commit b87ceec

File tree

1 file changed

+44
-1
lines changed
  • Node/quickstarts/auth-blocking-functions/functions

1 file changed

+44
-1
lines changed

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

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
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 admin = require("firebase-admin");
2325

2426
admin.initializeApp();
2527
const db = admin.firestore();
@@ -68,3 +70,44 @@ exports.checkforban = beforeUserSignedIn(async (event) => {
6870
// [END v2bannedHttpsError]
6971
});
7072
// [START v2CheckForBan]
73+
74+
// [START v2CheckEmailDomain]
75+
// [START v2beforeEmailSentFunctionTrigger]
76+
// Block email sending with any non-acme email address.
77+
exports.checkemaildomain = beforeEmailSent((event) => {
78+
// [END v2beforeEmailSentFunctionTrigger]
79+
// [START v2readEmailUser]
80+
// Email passed in from the CloudEvent.
81+
const email = event.data?.email || event.additionalUserInfo?.email;
82+
// [END v2readEmailUser]
83+
84+
// [START v2emailHttpsError]
85+
// Only users of a specific domain can receive emails.
86+
if (!email?.includes("@acme.com")) {
87+
// Throw an HttpsError so that Firebase Auth rejects the email sending.
88+
throw new HttpsError("invalid-argument", "Unauthorized email");
89+
}
90+
// [END v2emailHttpsError]
91+
});
92+
// [END v2CheckEmailDomain]
93+
94+
// [START v2CheckPhoneNumber]
95+
// [START v2beforeSmsSentFunctionTrigger]
96+
// Block SMS sending with any non-US phone number.
97+
exports.checkphonenumber = beforeSmsSent((event) => {
98+
// [END v2beforeSmsSentFunctionTrigger]
99+
// [START v2readSmsUser]
100+
// Phone number passed from the CloudEvent.
101+
const phoneNumber = event.data?.phoneNumber ||
102+
event.additionalUserInfo?.phoneNumber;
103+
// [END v2readSmsUser]
104+
105+
// [START v2smsHttpsError]
106+
// Only users of a specific region can receive SMS.
107+
if (!phoneNumber?.startsWith("+1")) {
108+
// Throw an HttpsError so that Firebase Auth rejects the SMS sending.
109+
throw new HttpsError("invalid-argument", "Unauthorized phone number");
110+
}
111+
// [END v2smsHttpsError]
112+
});
113+
// [END v2CheckPhoneNumber]

0 commit comments

Comments
 (0)