-
-
Notifications
You must be signed in to change notification settings - Fork 383
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(auth): prevent email enumeration during auth flows #1540
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ import { trpc } from "@/trpc/client"; | |
|
||
import { setToken } from "../actions"; | ||
import { registerNameFormSchema } from "./schema"; | ||
import { signIn } from "next-auth/react"; | ||
|
||
type RegisterNameFormValues = z.infer<typeof registerNameFormSchema>; | ||
|
||
|
@@ -39,24 +40,36 @@ export function RegisterNameForm() { | |
<Form {...form}> | ||
<form | ||
onSubmit={form.handleSubmit(async (data) => { | ||
const res = await registerUser.mutateAsync(data); | ||
try { | ||
const res = await registerUser.mutateAsync(data); | ||
|
||
if (res.ok) { | ||
await setToken(res.token); | ||
router.push("/register/verify"); | ||
} else { | ||
switch (res.reason) { | ||
case "emailNotAllowed": | ||
form.setError("email", { | ||
message: t("emailNotAllowed"), | ||
}); | ||
break; | ||
case "userAlreadyExists": | ||
form.setError("email", { | ||
message: t("userAlreadyExists"), | ||
}); | ||
break; | ||
if (res.ok) { | ||
await setToken(res.token); | ||
router.push("/register/verify"); | ||
} else if (res.reason === "emailNotAllowed") { | ||
form.setError("email", { | ||
message: t("emailNotAllowed"), | ||
}); | ||
} else if (res.reason === "userAlreadyExists") { | ||
// Attempt to sign in silently | ||
const signInResult = await signIn("email", { | ||
email: data.email, | ||
redirect: false, | ||
}); | ||
|
||
if (signInResult?.error) { | ||
throw new Error(signInResult.error); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @princesinghrajput looks like this is still unresolved? |
||
} | ||
|
||
// Only redirect if sign in was successful | ||
router.push("/login/verify"); | ||
} | ||
} catch (error) { | ||
form.setError("email", { | ||
message: t("authError", { | ||
defaultValue: "An error occurred during authentication", | ||
}), | ||
}); | ||
} | ||
})} | ||
> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,29 +86,30 @@ const providers: Provider[] = [ | |
return generateOtp(); | ||
}, | ||
async sendVerificationRequest({ identifier: email, token, url }) { | ||
const user = await prisma.user.findUnique({ | ||
where: { | ||
email, | ||
}, | ||
select: { | ||
name: true, | ||
locale: true, | ||
}, | ||
}); | ||
try { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this changes in this file should be reverted because:
|
||
const user = await prisma.user.findUnique({ | ||
where: { email }, | ||
select: { name: true, locale: true }, | ||
}); | ||
|
||
if (user) { | ||
await getEmailClient(user.locale ?? undefined).sendTemplate( | ||
"LoginEmail", | ||
{ | ||
// Send appropriate email based on whether user exists | ||
if (user) { | ||
await getEmailClient(user.locale ?? undefined).sendTemplate("LoginEmail", { | ||
to: email, | ||
props: { | ||
magicLink: absoluteUrl("/auth/login", { | ||
magicLink: url, | ||
}), | ||
magicLink: absoluteUrl("/auth/login", { magicLink: url }), | ||
code: token, | ||
}, | ||
}, | ||
); | ||
}); | ||
} else { | ||
await getEmailClient().sendTemplate("RegisterEmail", { | ||
to: email, | ||
props: { code: token }, | ||
}); | ||
} | ||
} catch (error) { | ||
console.error("Failed to send verification email:", error); | ||
throw new Error("Failed to send verification email"); | ||
} | ||
}, | ||
}), | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this change is necessary or relevant to this PR. Can you please revert this?