Skip to content

Commit b71c7de

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

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

  • Node/quickstarts/auth-blocking-functions/functions

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
const {
1818
beforeUserCreated,
1919
beforeUserSignedIn,
20+
beforeEmailSent,
21+
beforeSmsSent,
2022
HttpsError,
2123
} = require("firebase-functions/identity");
2224
const {admin} = require("firebase-admin");
@@ -68,3 +70,43 @@ 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+
// User data passed in from the CloudEvent.
81+
const user = event.data;
82+
// [END v2readEmailUser]
83+
84+
// [START v2emailHttpsError]
85+
// Only users of a specific domain can receive emails.
86+
if (!user?.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 user = event.data;
102+
// [END v2readSmsUser]
103+
104+
// [START v2smsHttpsError]
105+
// Only users of a specific region can receive SMS.
106+
if (!user?.phoneNumber?.startsWith("+1")) {
107+
// Throw an HttpsError so that Firebase Auth rejects the SMS sending.
108+
throw new HttpsError("invalid-argument", "Unauthorized phone number");
109+
}
110+
// [END v2smsHttpsError]
111+
});
112+
// [END v2CheckPhoneNumber]

0 commit comments

Comments
 (0)