From 1c97614eea4236677c8683c1b84ba637a713b7dd Mon Sep 17 00:00:00 2001 From: "Builder.io" Date: Mon, 10 Nov 2025 00:20:44 +0000 Subject: [PATCH] Handle existing email in Discord OAuth callback - redirect to link flow instead of failing cgen-79dca6287ad14c33b0b54d7103dad109 --- api/discord/oauth/callback.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/api/discord/oauth/callback.ts b/api/discord/oauth/callback.ts index a20fb7db..ee56cf53 100644 --- a/api/discord/oauth/callback.ts +++ b/api/discord/oauth/callback.ts @@ -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,