Prettier format pending files

This commit is contained in:
Builder.io 2025-11-10 00:48:56 +00:00
parent d2ac334f1d
commit c4ccf8e89e
2 changed files with 47 additions and 19 deletions

View file

@ -66,25 +66,46 @@ export default async function handler(req: any, res: any) {
const tokenParts = accessToken.split("."); const tokenParts = accessToken.split(".");
if (tokenParts.length === 3) { if (tokenParts.length === 3) {
try { try {
const payload = JSON.parse(Buffer.from(tokenParts[1], "base64").toString()); const payload = JSON.parse(
Buffer.from(tokenParts[1], "base64").toString(),
);
authenticatedUserId = payload.sub; authenticatedUserId = payload.sub;
console.log("[Discord OAuth] Successfully extracted user ID from token:", authenticatedUserId); console.log(
"[Discord OAuth] Successfully extracted user ID from token:",
authenticatedUserId,
);
} catch (decodeError) { } catch (decodeError) {
console.error("[Discord OAuth] Failed to decode JWT payload:", decodeError); console.error(
"[Discord OAuth] Failed to decode JWT payload:",
decodeError,
);
} }
} else { } else {
console.error("[Discord OAuth] Token does not have 3 parts:", tokenParts.length); console.error(
"[Discord OAuth] Token does not have 3 parts:",
tokenParts.length,
);
} }
} else { } else {
console.warn("[Discord OAuth] No sb-access-token cookie found in request"); console.warn(
console.log("[Discord OAuth] Available cookies:", cookie.substring(0, 200)); "[Discord OAuth] No sb-access-token cookie found in request",
);
console.log(
"[Discord OAuth] Available cookies:",
cookie.substring(0, 200),
);
} }
} catch (e) { } catch (e) {
console.error("[Discord OAuth] Error extracting user ID from cookies:", e); console.error(
"[Discord OAuth] Error extracting user ID from cookies:",
e,
);
} }
if (!authenticatedUserId) { if (!authenticatedUserId) {
console.error("[Discord OAuth] Linking flow but no authenticated user found"); console.error(
"[Discord OAuth] Linking flow but no authenticated user found",
);
// Redirect to login with a helpful message // Redirect to login with a helpful message
return res.redirect( return res.redirect(
`/login?error=not_authenticated&message=${encodeURIComponent("Please sign in before linking Discord")}`, `/login?error=not_authenticated&message=${encodeURIComponent("Please sign in before linking Discord")}`,
@ -162,7 +183,10 @@ export default async function handler(req: any, res: any) {
// LINKING FLOW: Link Discord to authenticated user // LINKING FLOW: Link Discord to authenticated user
if (isLinkingFlow && authenticatedUserId) { if (isLinkingFlow && authenticatedUserId) {
console.log("[Discord OAuth] Linking Discord to user:", authenticatedUserId); console.log(
"[Discord OAuth] Linking Discord to user:",
authenticatedUserId,
);
// Check if Discord ID is already linked to someone else // Check if Discord ID is already linked to someone else
const { data: existingLink } = await supabase const { data: existingLink } = await supabase
@ -172,20 +196,20 @@ export default async function handler(req: any, res: any) {
.single(); .single();
if (existingLink && existingLink.user_id !== authenticatedUserId) { if (existingLink && existingLink.user_id !== authenticatedUserId) {
console.error("[Discord OAuth] Discord ID already linked to different user"); console.error(
"[Discord OAuth] Discord ID already linked to different user",
);
return res.redirect( return res.redirect(
`/dashboard?error=already_linked&message=${encodeURIComponent("This Discord account is already linked to another AeThex account")}`, `/dashboard?error=already_linked&message=${encodeURIComponent("This Discord account is already linked to another AeThex account")}`,
); );
} }
// Create or update Discord link // Create or update Discord link
const { error: linkError } = await supabase const { error: linkError } = await supabase.from("discord_links").upsert({
.from("discord_links") discord_id: discordUser.id,
.upsert({ user_id: authenticatedUserId,
discord_id: discordUser.id, linked_at: new Date().toISOString(),
user_id: authenticatedUserId, });
linked_at: new Date().toISOString(),
});
if (linkError) { if (linkError) {
console.error("[Discord OAuth] Link creation failed:", linkError); console.error("[Discord OAuth] Link creation failed:", linkError);
@ -194,7 +218,10 @@ export default async function handler(req: any, res: any) {
); );
} }
console.log("[Discord OAuth] Successfully linked Discord:", discordUser.id); console.log(
"[Discord OAuth] Successfully linked Discord:",
discordUser.id,
);
return res.redirect(redirectTo); return res.redirect(redirectTo);
} }

View file

@ -13,7 +13,8 @@ export default function handler(req: any, res: any) {
} }
// Get the current API base from the request origin // Get the current API base from the request origin
const protocol = req.headers["x-forwarded-proto"] || req.headers.protocol || "https"; const protocol =
req.headers["x-forwarded-proto"] || req.headers.protocol || "https";
const host = req.headers["x-forwarded-host"] || req.headers.host; const host = req.headers["x-forwarded-host"] || req.headers.host;
const apiBase = `${protocol}://${host}`; const apiBase = `${protocol}://${host}`;