Prettier format pending files
This commit is contained in:
parent
292015fead
commit
8ef1c8e6dd
2 changed files with 67 additions and 41 deletions
|
|
@ -44,19 +44,22 @@ 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(
|
||||||
method: "POST",
|
"https://discord.com/api/v10/oauth2/token",
|
||||||
headers: {
|
{
|
||||||
"Content-Type": "application/x-www-form-urlencoded",
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/x-www-form-urlencoded",
|
||||||
|
},
|
||||||
|
body: new URLSearchParams({
|
||||||
|
client_id: clientId,
|
||||||
|
client_secret: clientSecret,
|
||||||
|
grant_type: "authorization_code",
|
||||||
|
code,
|
||||||
|
redirect_uri: redirectUri,
|
||||||
|
}).toString(),
|
||||||
},
|
},
|
||||||
body: new URLSearchParams({
|
);
|
||||||
client_id: clientId,
|
|
||||||
client_secret: clientSecret,
|
|
||||||
grant_type: "authorization_code",
|
|
||||||
code,
|
|
||||||
redirect_uri: redirectUri,
|
|
||||||
}).toString(),
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!tokenResponse.ok) {
|
if (!tokenResponse.ok) {
|
||||||
const errorData = await tokenResponse.json();
|
const errorData = await tokenResponse.json();
|
||||||
|
|
@ -114,19 +117,23 @@ 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 } =
|
||||||
email: discordUser.email,
|
await supabase.auth.admin.createUser({
|
||||||
email_confirm: true,
|
email: discordUser.email,
|
||||||
user_metadata: {
|
email_confirm: true,
|
||||||
full_name: discordUser.username,
|
user_metadata: {
|
||||||
avatar_url: discordUser.avatar
|
full_name: discordUser.username,
|
||||||
? `https://cdn.discordapp.com/avatars/${discordUser.id}/${discordUser.avatar}.png`
|
avatar_url: discordUser.avatar
|
||||||
: null,
|
? `https://cdn.discordapp.com/avatars/${discordUser.id}/${discordUser.avatar}.png`
|
||||||
},
|
: null,
|
||||||
});
|
},
|
||||||
|
});
|
||||||
|
|
||||||
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,18 +141,25 @@ 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
|
||||||
id: userId,
|
.from("user_profiles")
|
||||||
email: discordUser.email,
|
.insert({
|
||||||
full_name: discordUser.username,
|
id: userId,
|
||||||
avatar_url: discordUser.avatar
|
email: discordUser.email,
|
||||||
? `https://cdn.discordapp.com/avatars/${discordUser.id}/${discordUser.avatar}.png`
|
full_name: discordUser.username,
|
||||||
: null,
|
avatar_url: discordUser.avatar
|
||||||
});
|
? `https://cdn.discordapp.com/avatars/${discordUser.id}/${discordUser.avatar}.png`
|
||||||
|
: null,
|
||||||
|
});
|
||||||
|
|
||||||
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,13 +173,16 @@ 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 } =
|
||||||
user_id: userId,
|
await supabase.auth.admin.createSession({
|
||||||
});
|
user_id: userId,
|
||||||
|
});
|
||||||
|
|
||||||
if (sessionError || !sessionData.session) {
|
if (sessionError || !sessionData.session) {
|
||||||
console.error("[Discord OAuth] Session creation failed:", sessionError);
|
console.error("[Discord OAuth] Session creation failed:", sessionError);
|
||||||
|
|
@ -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,
|
||||||
|
|
|
||||||
|
|
@ -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",
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue