Prettier format pending files

This commit is contained in:
Builder.io 2025-11-09 08:16:06 +00:00
parent 292015fead
commit 8ef1c8e6dd
2 changed files with 67 additions and 41 deletions

View file

@ -44,7 +44,9 @@ export default async function handler(req: any, res: any) {
const redirectUri = `${process.env.VITE_API_BASE || "https://aethex.dev"}/discord`; const redirectUri = `${process.env.VITE_API_BASE || "https://aethex.dev"}/discord`;
// Exchange code for access token // Exchange code for access token
const tokenResponse = await fetch("https://discord.com/api/v10/oauth2/token", { const tokenResponse = await fetch(
"https://discord.com/api/v10/oauth2/token",
{
method: "POST", method: "POST",
headers: { headers: {
"Content-Type": "application/x-www-form-urlencoded", "Content-Type": "application/x-www-form-urlencoded",
@ -56,7 +58,8 @@ export default async function handler(req: any, res: any) {
code, code,
redirect_uri: redirectUri, redirect_uri: redirectUri,
}).toString(), }).toString(),
}); },
);
if (!tokenResponse.ok) { if (!tokenResponse.ok) {
const errorData = await tokenResponse.json(); const errorData = await tokenResponse.json();
@ -114,7 +117,8 @@ export default async function handler(req: any, res: any) {
} else { } else {
// Create new user // Create new user
// First create auth user // First create auth user
const { data: authData, error: authError } = await supabase.auth.admin.createUser({ const { data: authData, error: authError } =
await supabase.auth.admin.createUser({
email: discordUser.email, email: discordUser.email,
email_confirm: true, email_confirm: true,
user_metadata: { user_metadata: {
@ -126,7 +130,10 @@ export default async function handler(req: any, res: any) {
}); });
if (authError || !authData.user) { if (authError || !authData.user) {
console.error("[Discord OAuth] Auth user creation failed:", authError); console.error(
"[Discord OAuth] Auth user creation failed:",
authError,
);
return res.status(500).json({ message: "Failed to create account" }); return res.status(500).json({ message: "Failed to create account" });
} }
@ -134,7 +141,9 @@ export default async function handler(req: any, res: any) {
isNewUser = true; isNewUser = true;
// Create user profile // Create user profile
const { error: profileError } = await supabase.from("user_profiles").insert({ const { error: profileError } = await supabase
.from("user_profiles")
.insert({
id: userId, id: userId,
email: discordUser.email, email: discordUser.email,
full_name: discordUser.username, full_name: discordUser.username,
@ -144,8 +153,13 @@ export default async function handler(req: any, res: any) {
}); });
if (profileError) { if (profileError) {
console.error("[Discord OAuth] Profile creation failed:", profileError); console.error(
return res.status(500).json({ message: "Failed to create user profile" }); "[Discord OAuth] Profile creation failed:",
profileError,
);
return res
.status(500)
.json({ message: "Failed to create user profile" });
} }
} }
} }
@ -159,11 +173,14 @@ export default async function handler(req: any, res: any) {
if (linkError) { if (linkError) {
console.error("[Discord OAuth] Link creation failed:", linkError); console.error("[Discord OAuth] Link creation failed:", linkError);
return res.status(500).json({ message: "Failed to link Discord account" }); return res
.status(500)
.json({ message: "Failed to link Discord account" });
} }
// Generate session token // Generate session token
const { data: sessionData, error: sessionError } = await supabase.auth.admin.createSession({ const { data: sessionData, error: sessionError } =
await supabase.auth.admin.createSession({
user_id: userId, user_id: userId,
}); });
@ -181,7 +198,9 @@ export default async function handler(req: any, res: any) {
return res.status(200).json({ return res.status(200).json({
success: true, success: true,
message: isNewUser ? "Account created successfully" : "Linked successfully", message: isNewUser
? "Account created successfully"
: "Linked successfully",
session: { session: {
access_token: sessionData.session.access_token, access_token: sessionData.session.access_token,
refresh_token: sessionData.session.refresh_token, refresh_token: sessionData.session.refresh_token,

View file

@ -12,7 +12,9 @@ export default function DiscordOAuthCallback() {
const [searchParams] = useSearchParams(); const [searchParams] = useSearchParams();
const { user, signIn } = useAuth(); const { user, signIn } = useAuth();
const [status, setStatus] = useState<"loading" | "success" | "error">("loading"); const [status, setStatus] = useState<"loading" | "success" | "error">(
"loading",
);
const [message, setMessage] = useState("Connecting to Discord..."); const [message, setMessage] = useState("Connecting to Discord...");
useEffect(() => { useEffect(() => {
@ -66,7 +68,12 @@ export default function DiscordOAuthCallback() {
} }
// Redirect to next page // Redirect to next page
const nextPath = state && state.startsWith("/") ? state : data.isNewUser ? "/onboarding" : "/dashboard"; const nextPath =
state && state.startsWith("/")
? state
: data.isNewUser
? "/onboarding"
: "/dashboard";
setTimeout(() => { setTimeout(() => {
navigate(nextPath); navigate(nextPath);
window.location.reload(); // Reload to pick up new auth context window.location.reload(); // Reload to pick up new auth context
@ -76,7 +83,7 @@ export default function DiscordOAuthCallback() {
setMessage( setMessage(
error instanceof Error error instanceof Error
? error.message ? error.message
: "An unexpected error occurred" : "An unexpected error occurred",
); );
} }
}; };