import { useState, type FormEvent } from "react"; import { useNavigate } from "react-router"; import { useAuth } from "../contexts/AuthContext"; import { Masthead } from "../components/Masthead"; type AuthMode = "signin" | "signup"; export default function LoginPage() { const navigate = useNavigate(); const { signIn, signUp, isAuthConfigured, isAuthenticated } = useAuth(); const [mode, setMode] = useState("signin"); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [error, setError] = useState(null); const [loading, setLoading] = useState(false); const [message, setMessage] = useState(null); // Redirect if already authenticated if (isAuthenticated && isAuthConfigured) { navigate("/mesh"); return null; } const handleEmailAuth = async (e: FormEvent) => { e.preventDefault(); setError(null); setMessage(null); setLoading(true); try { if (mode === "signin") { const { error } = await signIn(email, password); if (error) { setError(error.message); } else { navigate("/mesh"); } } else if (mode === "signup") { const { error } = await signUp(email, password); if (error) { setError(error.message); } else { setMessage("Check your email for a confirmation link."); } } } finally { setLoading(false); } }; // If auth is not configured, show a message if (!isAuthConfigured) { return (

Authentication Required

Authentication is not configured. Please configure Supabase authentication to use this application.

For API access, use an API key in request headers instead.

); } return (

Sign In

{mode === "signin" && "Sign in to your account"} {mode === "signup" && "Create a new account"}

{/* Mode switcher */}
{/* Email/password form */}
setEmail(e.target.value)} placeholder="you@example.com" className="w-full px-3 py-2 bg-zinc-900 border border-zinc-800 rounded text-white placeholder-zinc-600 focus:outline-none focus:border-zinc-600" required />
setPassword(e.target.value)} placeholder="********" className="w-full px-3 py-2 bg-zinc-900 border border-zinc-800 rounded text-white placeholder-zinc-600 focus:outline-none focus:border-zinc-600" required minLength={6} />
{error &&
{error}
} {message &&
{message}
}
); }