Update Discord linking flow to use OAuth Federation
cgen-ad2e373cde4344ac9232bef944ff782d
This commit is contained in:
parent
f607d58b4e
commit
2a885a7ec1
1 changed files with 17 additions and 33 deletions
|
|
@ -173,50 +173,34 @@ export default async function handler(req: any, res: any) {
|
||||||
// Initialize Supabase client with service role
|
// Initialize Supabase client with service role
|
||||||
const supabase = getAdminClient();
|
const supabase = getAdminClient();
|
||||||
|
|
||||||
// LINKING FLOW: Link Discord to authenticated user
|
// LINKING FLOW: Link Discord to authenticated user's Foundation Passport
|
||||||
if (isLinkingFlow && authenticatedUserId) {
|
if (isLinkingFlow && authenticatedUserId) {
|
||||||
console.log(
|
console.log(
|
||||||
"[Discord OAuth] Linking Discord to user:",
|
"[Discord OAuth] Linking Discord to user:",
|
||||||
authenticatedUserId,
|
authenticatedUserId,
|
||||||
);
|
);
|
||||||
|
|
||||||
// Check if Discord ID is already linked to someone else
|
try {
|
||||||
const { data: existingLink } = await supabase
|
await linkProviderToPassport(authenticatedUserId, "discord", {
|
||||||
.from("discord_links")
|
id: discordUser.id,
|
||||||
.select("user_id")
|
email: discordUser.email,
|
||||||
.eq("discord_id", discordUser.id)
|
username: discordUser.username,
|
||||||
.single();
|
avatar: discordUser.avatar ? `https://cdn.discordapp.com/avatars/${discordUser.id}/${discordUser.avatar}.webp` : undefined,
|
||||||
|
});
|
||||||
|
|
||||||
if (existingLink && existingLink.user_id !== authenticatedUserId) {
|
console.log(
|
||||||
console.error(
|
"[Discord OAuth] Successfully linked Discord to user:",
|
||||||
"[Discord OAuth] Discord ID already linked to different user",
|
authenticatedUserId,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
await notifyAccountLinked(authenticatedUserId, "Discord");
|
||||||
|
return res.redirect(redirectTo);
|
||||||
|
} catch (linkError: any) {
|
||||||
|
console.error("[Discord OAuth] Linking failed:", linkError);
|
||||||
return res.redirect(
|
return res.redirect(
|
||||||
`/dashboard?error=already_linked&message=${encodeURIComponent("This Discord account is already linked to another AeThex account")}`,
|
`/dashboard?error=link_failed&message=${encodeURIComponent(linkError?.message || "Failed to link Discord account")}`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create or update Discord link
|
|
||||||
const { error: linkError } = await supabase.from("discord_links").upsert({
|
|
||||||
discord_id: discordUser.id,
|
|
||||||
user_id: authenticatedUserId,
|
|
||||||
linked_at: new Date().toISOString(),
|
|
||||||
});
|
|
||||||
|
|
||||||
if (linkError) {
|
|
||||||
console.error("[Discord OAuth] Link creation failed:", linkError);
|
|
||||||
return res.redirect(
|
|
||||||
`/dashboard?error=link_failed&message=${encodeURIComponent("Failed to link Discord account")}`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log(
|
|
||||||
"[Discord OAuth] Successfully linked Discord:",
|
|
||||||
discordUser.id,
|
|
||||||
);
|
|
||||||
|
|
||||||
await notifyAccountLinked(authenticatedUserId, "Discord");
|
|
||||||
return res.redirect(redirectTo);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// LOGIN FLOW: OAuth Federation
|
// LOGIN FLOW: OAuth Federation
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue