Handle existing email in Discord OAuth callback - redirect to link flow instead of failing

cgen-79dca6287ad14c33b0b54d7103dad109
This commit is contained in:
Builder.io 2025-11-10 00:20:44 +00:00
parent 07a78a05c0
commit 1c97614eea

View file

@ -163,6 +163,20 @@ export default async function handler(req: any, res: any) {
});
if (authError || !authData.user) {
// Check if error is because email already exists
if (
authError?.message &&
authError.message.toLowerCase().includes("already")
) {
console.log(
"[Discord OAuth] Email already registered, redirecting to link flow",
);
// Redirect to login page where user can link Discord to existing account
return res.redirect(
`/login?error=account_exists&message=${encodeURIComponent("Please sign in first, then link your Discord account from settings")}`,
);
}
console.error("[Discord OAuth] Auth user creation failed:", {
email: discordUser.email,
username: discordUser.username,