Skip to content

Commit

Permalink
chore: add additional guard clause for email validation
Browse files Browse the repository at this point in the history
  • Loading branch information
dcshzj committed Nov 7, 2024
1 parent 0dfe78c commit 805eebf
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion apps/studio/src/server/modules/whitelist/whitelist.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import { TRPCError } from "@trpc/server"

import { isValidEmail } from "~/utils/email"
import { db } from "../database"

export const isEmailWhitelisted = async (email: string) => {
const lowercaseEmail = email.toLowerCase()

// Extra guard even if Zod validation has already checked
if (!isValidEmail(lowercaseEmail)) {
throw new TRPCError({
code: "BAD_REQUEST",
message: "Please sign in with a valid email address.",
})
}

// Step 1: Check if the exact email address is whitelisted
const exactMatch = await db
.selectFrom("Whitelist")
Expand All @@ -24,7 +33,7 @@ export const isEmailWhitelisted = async (email: string) => {
if (emailParts.length !== 2 && !emailParts[1]) {
throw new TRPCError({
code: "BAD_REQUEST",
message: "An invalid email was provided",
message: "Please sign in with a valid email address.",
})
}

Expand Down

0 comments on commit 805eebf

Please sign in to comment.