Enhance `api/discord/verify-code.ts` with additional console logs to aid in debugging Supabase client creation, code lookup, and general error handling, including more detailed error information in the response. Replit-Commit-Author: Agent Replit-Commit-Session-Id: 9203795e-937a-4306-b81d-b4d5c78c240e Replit-Commit-Checkpoint-Type: full_checkpoint Replit-Commit-Event-Id: 409d1922-375f-4f4a-9fbf-f19ce19c2a34 Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/7c94b7a0-29c7-4f2e-94ef-44b2153872b7/9203795e-937a-4306-b81d-b4d5c78c240e/saoW2ee Replit-Helium-Checkpoint-Created: true
135 lines
4 KiB
TypeScript
135 lines
4 KiB
TypeScript
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" });
|
|
}
|
|
|
|
// Try both possible env var names for backwards compatibility
|
|
const supabaseUrl = process.env.SUPABASE_URL || process.env.VITE_SUPABASE_URL;
|
|
const supabaseServiceRole = process.env.SUPABASE_SERVICE_ROLE;
|
|
|
|
if (!supabaseUrl || !supabaseServiceRole) {
|
|
console.error("[Discord Verify] Missing env vars:", {
|
|
supabaseUrl: !!supabaseUrl,
|
|
supabaseServiceRole: !!supabaseServiceRole,
|
|
});
|
|
return res.status(500).json({ message: "Server configuration error" });
|
|
}
|
|
|
|
try {
|
|
console.log("[Discord Verify] Creating Supabase client with URL:", supabaseUrl?.substring(0, 30) + "...");
|
|
const supabase = createClient(supabaseUrl, supabaseServiceRole);
|
|
|
|
console.log("[Discord Verify] Looking up code:", verification_code.trim());
|
|
|
|
// 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();
|
|
|
|
console.log("[Discord Verify] Query result:", {
|
|
found: !!verification,
|
|
error: verifyError?.message,
|
|
code: verifyError?.code
|
|
});
|
|
|
|
if (verifyError) {
|
|
console.error("[Discord Verify] Code lookup failed:", {
|
|
code: verification_code.trim(),
|
|
error: verifyError,
|
|
});
|
|
return res.status(400).json({
|
|
message:
|
|
"Invalid or expired verification code. Please try /verify again.",
|
|
error: verifyError.message,
|
|
});
|
|
}
|
|
|
|
if (!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
|
|
const { error: deleteError } = await supabase
|
|
.from("discord_verifications")
|
|
.delete()
|
|
.eq("verification_code", verification_code.trim());
|
|
|
|
if (deleteError) {
|
|
console.error(
|
|
"[Discord Verify] Failed to delete verification code:",
|
|
deleteError,
|
|
);
|
|
// Don't return error - code is already used and link is created
|
|
}
|
|
|
|
res.status(200).json({
|
|
success: true,
|
|
message: "Discord account linked successfully!",
|
|
discord_user: {
|
|
id: discordId,
|
|
username: verification.username || "Discord User",
|
|
discriminator: "0000",
|
|
},
|
|
});
|
|
} catch (error: any) {
|
|
console.error("[Discord Verify] Error:", {
|
|
message: error?.message,
|
|
code: error?.code,
|
|
stack: error?.stack?.substring(0, 500),
|
|
});
|
|
res.status(500).json({
|
|
message: "An error occurred. Please try again.",
|
|
debug: process.env.NODE_ENV === 'development' ? error?.message : undefined,
|
|
});
|
|
}
|
|
}
|