import { useState, useEffect } from "react"; 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, auto-fill it if (code) { setVerificationCode(code); handleVerify(code); } }, [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); } navigate("/login?next=/discord-verify"); } }, [user, navigate, code]); // On component mount, check if code was stored in sessionStorage useEffect(() => { if (user && !code) { const storedCode = sessionStorage.getItem("discord_verification_code"); if (storedCode) { setVerificationCode(storedCode); handleVerify(storedCode); sessionStorage.removeItem("discord_verification_code"); } } }, [user]); 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/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("/profile/settings"); }, 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 ({discordUser.username}) has been linked to your AeThex profile.

Discord User

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

Redirecting to your profile settings...

) : ( // Input State

How to get your code:

  1. Open Discord
  2. Go to any server where the AeThex bot is installed
  3. Type{" "} /verify
  4. Copy the 6-digit code from the bot's response
{error && ( Verification Failed {error} )}
setVerificationCode(e.target.value)} maxLength={6} disabled={isLoading} className="text-center text-lg tracking-widest" />
)}
{/* Info Box */}

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

); }