import { useState, useEffect } from "react"; const API_BASE = import.meta.env.VITE_API_BASE || ""; import { useNavigate, useSearchParams } from "react-router-dom"; import { useAuth } from "@/contexts/AuthContext"; import Layout from "@/components/Layout"; import SEO from "@/components/SEO"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"; import { AlertTriangle, CheckCircle, Loader } from "lucide-react"; export default function DiscordVerify() { const navigate = useNavigate(); const [searchParams] = useSearchParams(); const { user } = useAuth(); const [verificationCode, setVerificationCode] = useState(""); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); const [success, setSuccess] = useState(false); const [discordUser, setDiscordUser] = useState(null); const code = searchParams.get("code"); useEffect(() => { // If code in URL, store it but don't verify yet (need user to be authenticated first) if (code) { setVerificationCode(code); // Don't call handleVerify here - wait for user authentication below } }, [code]); useEffect(() => { // Redirect if not authenticated, preserving the code param if (!user) { // Store code in sessionStorage so we can retrieve it after login if (code) { sessionStorage.setItem("discord_verification_code", code); // Redirect to login with the code preserved in the URL navigate(`/login?next=/discord-verify?code=${code}`); } else { // No code, redirect to regular login navigate("/login?next=/discord-verify"); } } }, [user, navigate, code]); // Auto-verify once user is authenticated useEffect(() => { // Case 1: Code in URL and user is now authenticated if (user && code && verificationCode && !isLoading) { handleVerify(verificationCode); } // Case 2: Code in sessionStorage (from redirect back from login) and user is now authenticated else if (user && !code && !isLoading) { const storedCode = sessionStorage.getItem("discord_verification_code"); if (storedCode) { setVerificationCode(storedCode); handleVerify(storedCode); sessionStorage.removeItem("discord_verification_code"); } } }, [user, isLoading]); const handleVerify = async (codeToVerify: string) => { if (!codeToVerify.trim()) { setError("Please enter a verification code"); return; } setIsLoading(true); setError(null); setDiscordUser(null); try { const response = await fetch(`${API_BASE}/api/discord/verify-code`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ verification_code: codeToVerify.trim(), user_id: user?.id, }), }); const data = await response.json(); if (!response.ok) { setError(data.message || "Verification failed. Please try again."); setIsLoading(false); return; } // Success setDiscordUser(data.discord_user); setSuccess(true); setVerificationCode(""); // Redirect to dashboard after 3 seconds setTimeout(() => { navigate("/dashboard?tab=connections"); }, 3000); } catch (err) { setError( err instanceof Error ? err.message : "An error occurred. Please try again.", ); setIsLoading(false); } }; if (!user) { return null; } return (
Link Discord Account
{success && discordUser ? ( // Success State
Successfully Linked! Your Discord account has been connected to your AeThex profile.

Discord Account

{discordUser.username} #{discordUser.discriminator || "0000"}

Redirecting to your profile settings...

) : ( // Input State

📋 How to get your code:

  1. 1. Open Discord
  2. 2. Go to any server where the AeThex bot is installed
  3. 3. Type /verify
  4. 4. Copy the 6-digit code from the bot's response
{error && ( Verification Failed {error} )}
setVerificationCode(e.target.value.toUpperCase().replace(/[^A-Z0-9]/g, '').slice(0, 6))} maxLength={6} disabled={isLoading} className="text-center text-3xl font-bold tracking-[0.5em] border-2 border-indigo-500/50 focus:border-indigo-500 bg-background/60 hover:bg-background/80 transition-colors" />

Enter the 6-character code from Discord

)}
{/* Info Box */}

💡 Tip: You can also sign in directly with Discord on the login page if you're creating a new account.

); }