import { useState, useEffect } from "react"; import { useNavigate, Link, useLocation } from "react-router-dom"; import { aethexUserService } from "@/lib/aethex-database-adapter"; import { useAuth } from "@/contexts/AuthContext"; import { useAethexToast } from "@/hooks/use-aethex-toast"; import Layout from "@/components/Layout"; import SEO from "@/components/SEO"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Badge } from "@/components/ui/badge"; import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"; import LoadingScreen from "@/components/LoadingScreen"; import { LogIn, ArrowRight, Shield, Sparkles, Github, Mail, Lock, User, Info, } from "lucide-react"; import { Dialog, DialogTrigger, DialogContent, DialogHeader, DialogTitle, DialogDescription, DialogFooter, DialogClose, } from "@/components/ui/dialog"; function OrgLogin() { const [email, setEmail] = useState(""); const [sending, setSending] = useState(false); const [sent, setSent] = useState(null); const [error, setError] = useState(null); const isValid = /@aethex\.dev$/i.test(email); return (
Aethex Login (org)
@aethex.dev
{sent ? ( Check your inbox We sent a magic link to {email}. If email isn’t configured, a manual link is shown below. {sent.startsWith("http") && (

{sent}

)}
) : null} {error ? ( Request failed {error} ) : null}
setEmail(e.target.value)} />
); } export default function Login() { const [isLoading, setIsLoading] = useState(false); const [isSignUp, setIsSignUp] = useState(false); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [fullName, setFullName] = useState(""); const [manualVerificationLink, setManualVerificationLink] = useState< string | null >(null); const [showReset, setShowReset] = useState(false); const [resetEmail, setResetEmail] = useState(""); const navigate = useNavigate(); const location = useLocation(); const { signIn, signUp, signInWithOAuth, user, loading, profileComplete, requestPasswordReset, } = useAuth(); const { info: toastInfo, error: toastError } = useAethexToast(); // After auth resolves and a user exists, navigate to next path or dashboard useEffect(() => { if (!loading && user) { const params = new URLSearchParams(location.search); const next = params.get("next"); const safeNext = next && next.startsWith("/") ? next : null; navigate(safeNext || (profileComplete ? "/dashboard" : "/onboarding"), { replace: true, }); } }, [user, loading, profileComplete, navigate, location.search]); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setIsLoading(true); try { if (isSignUp) { const result = await signUp(email, password, { id: "", full_name: fullName, user_type: "game_developer", username: email.split("@")[0], }); if (result?.emailSent) { setManualVerificationLink(null); } else if (result?.verificationUrl) { setManualVerificationLink(result.verificationUrl); try { if ( typeof navigator !== "undefined" && navigator.clipboard?.writeText ) { await navigator.clipboard.writeText(result.verificationUrl); toastInfo({ title: "Verification link copied", description: "We copied the manual verification link to your clipboard. Paste it into your browser to finish signup.", }); } else { throw new Error("clipboard unsupported"); } } catch { toastInfo({ title: "Manual verification required", description: "Copy the link shown in the banner to verify your account.", }); } } setIsSignUp(false); } else { await signIn(email, password); // Do not navigate immediately; wait for auth state to update } } catch (error: any) { console.error("Authentication error:", error); toastError({ title: "Authentication failed", description: error?.message || "Something went wrong. Please try again.", }); } finally { setIsLoading(false); } }; const handleSocialLogin = async (provider: "github" | "google") => { setIsLoading(true); try { await signInWithOAuth(provider); } catch (error: any) { console.error(`${provider} authentication error:`, error); } finally { setIsLoading(false); } }; // Show loading screen only during form submission, not during auth context loading if (isLoading && !loading) { return ( ); } // If auth context is still loading, show a different loading state if (loading) { return ( ); } return ( <>
{/* Floating particles effect */}
{[...Array(20)].map((_, i) => (
))}
{isSignUp ? "Create Account" : "Welcome Back"} {isSignUp ? "Create your AeThex account to get started" : "Sign in to your AeThex account to access the dashboard"}
Secure Login
{manualVerificationLink ? ( Manual verification required

We couldn't send the verification email automatically. Use the link below to confirm your account:

{manualVerificationLink}

) : null} {/* Social Login Buttons */}
Or continue with email
{/* Aethex Org Login (Magic Link) */} {/* Email/Password Form */}
{isSignUp && (
setFullName(e.target.value)} placeholder="Enter your full name" className="pl-10 bg-background/50 border-border/50 focus:border-aethex-400" required={isSignUp} />
)}
setEmail(e.target.value)} placeholder="Enter your email" className="pl-10 bg-background/50 border-border/50 focus:border-aethex-400" required />
setPassword(e.target.value)} placeholder={ isSignUp ? "Create a password" : "Enter your password" } className="pl-10 bg-background/50 border-border/50 focus:border-aethex-400" required minLength={isSignUp ? 6 : undefined} />
{isSignUp && (

Password must be at least 6 characters long

)}
{!isSignUp && (
)}

{isSignUp ? "Already have an account?" : "Don't have an account?"}{" "}

{/* Security Notice */}

🔒 Your data is protected with enterprise-grade security

Reset your password Enter the email associated with your account. We'll send a reset link.
setResetEmail(e.target.value)} placeholder="you@example.com" />
); }