Skip to content

Commit

Permalink
Update send-otp.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
steven-tey committed Dec 22, 2024
1 parent f506bf0 commit f20c674
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions apps/web/lib/actions/send-otp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { ratelimit, redis } from "@/lib/upstash";
import { prisma } from "@dub/prisma";
import { get } from "@vercel/edge-config";
import { sendEmail } from "emails";
import VerifyEmail from "emails/verify-email";
import { flattenValidationErrors } from "next-safe-action";
Expand Down Expand Up @@ -33,22 +34,37 @@ export const sendOtpAction = actionClient
throw new Error("Too many requests. Please try again later.");
}

if (email.includes("+") && email.endsWith("@gmail.com")) {
throw new Error(
"Email addresses with + are not allowed. Please use your work email instead.",
);
}

const domain = email.split("@")[1];
const isDisposable = await redis.sismember(
"disposableEmailDomains",
domain,
);

const [isDisposable, emailDomainTerms] = await Promise.all([
redis.sismember("disposableEmailDomains", domain),
get("emailDomainTerms"),
]);

if (isDisposable) {
throw new Error(
"Disposable email addresses are not allowed. If you think this is a mistake, please contact us at support@dub.co",
"Invalid email address – please use your work email instead. If you think this is a mistake, please contact us at support@dub.co",
);
}

if (email.includes("+") && email.endsWith("@gmail.com")) {
throw new Error(
"Email addresses with + are not allowed. Please use your work email instead.",
if (emailDomainTerms && Array.isArray(emailDomainTerms)) {
const blacklistedEmailDomainTermsRegex = new RegExp(
emailDomainTerms
.map((term: string) => term.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")) // replace special characters with escape sequences
.join("|"),
);

if (blacklistedEmailDomainTermsRegex.test(domain)) {
throw new Error(
"Invalid email address – please use your work email instead. If you think this is a mistake, please contact us at support@dub.co",
);
}
}

const code = generateOTP();
Expand Down

0 comments on commit f20c674

Please sign in to comment.