Create Discord verify code backend endpoint
cgen-c25ef29ca92b4de78e895411e4b84088
This commit is contained in:
parent
15ae4f8c64
commit
c23ef09897
1 changed files with 97 additions and 0 deletions
97
api/discord/verify-code.ts
Normal file
97
api/discord/verify-code.ts
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
import { createClient } from "@supabase/supabase-js";
|
||||
|
||||
export const config = {
|
||||
runtime: "nodejs",
|
||||
};
|
||||
|
||||
export default async function handler(req: any, res: any) {
|
||||
if (req.method !== "POST") {
|
||||
return res.status(405).json({ error: "Method not allowed" });
|
||||
}
|
||||
|
||||
const { verification_code, user_id } = req.body;
|
||||
|
||||
if (!verification_code || !user_id) {
|
||||
return res.status(400).json({ message: "Missing verification code or user ID" });
|
||||
}
|
||||
|
||||
const supabaseUrl = process.env.VITE_SUPABASE_URL;
|
||||
const supabaseServiceRole = process.env.SUPABASE_SERVICE_ROLE;
|
||||
|
||||
if (!supabaseUrl || !supabaseServiceRole) {
|
||||
return res.status(500).json({ message: "Server configuration error" });
|
||||
}
|
||||
|
||||
try {
|
||||
const supabase = createClient(supabaseUrl, supabaseServiceRole);
|
||||
|
||||
// Find valid verification code
|
||||
const { data: verification, error: verifyError } = await supabase
|
||||
.from("discord_verifications")
|
||||
.select("*")
|
||||
.eq("verification_code", verification_code.trim())
|
||||
.gt("expires_at", new Date().toISOString())
|
||||
.single();
|
||||
|
||||
if (verifyError || !verification) {
|
||||
return res.status(400).json({
|
||||
message: "Invalid or expired verification code. Please try /verify again.",
|
||||
});
|
||||
}
|
||||
|
||||
const discordId = verification.discord_id;
|
||||
|
||||
// Check if already linked
|
||||
const { data: existingLink } = await supabase
|
||||
.from("discord_links")
|
||||
.select("*")
|
||||
.eq("discord_id", discordId)
|
||||
.single();
|
||||
|
||||
if (existingLink && existingLink.user_id !== user_id) {
|
||||
return res.status(400).json({
|
||||
message: "This Discord account is already linked to another AeThex account.",
|
||||
});
|
||||
}
|
||||
|
||||
// Create or update link
|
||||
const { error: linkError } = await supabase.from("discord_links").upsert({
|
||||
discord_id: discordId,
|
||||
user_id: user_id,
|
||||
linked_at: new Date().toISOString(),
|
||||
});
|
||||
|
||||
if (linkError) {
|
||||
console.error("[Discord Verify] Link creation failed:", linkError);
|
||||
return res.status(500).json({ message: "Failed to link Discord account" });
|
||||
}
|
||||
|
||||
// Delete used verification code
|
||||
await supabase
|
||||
.from("discord_verifications")
|
||||
.delete()
|
||||
.eq("verification_code", verification_code.trim());
|
||||
|
||||
// Fetch Discord user info from verification for response
|
||||
const { data: discordUser } = await supabase
|
||||
.from("discord_verifications")
|
||||
.select("*")
|
||||
.eq("discord_id", discordId)
|
||||
.maybeSingle();
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
message: "Discord account linked successfully!",
|
||||
discord_user: {
|
||||
id: discordId,
|
||||
username: discordUser?.username || "Discord User",
|
||||
discriminator: "0000",
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("[Discord Verify] Error:", error);
|
||||
res.status(500).json({
|
||||
message: "An error occurred. Please try again.",
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue