"use client"; import { useState, type FormEvent } from "react"; import { useRouter } from "next/navigation"; import { useAuth } from "@/lib/auth"; import { Button } from "@/components/ui/Button"; import { Spinner } from "@/components/ui/Spinner"; export default function LoginPage() { const { user, loading: authLoading, login, register } = useAuth(); const router = useRouter(); const [isRegister, setIsRegister] = useState(false); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [firstName, setFirstName] = useState(""); const [lastName, setLastName] = useState(""); const [error, setError] = useState(""); const [loading, setLoading] = useState(false); // Redirect if already logged in if (!authLoading && user) { router.replace("/dashboard"); return null; } async function handleSubmit(e: FormEvent) { e.preventDefault(); setError(""); setLoading(true); try { if (isRegister) { await register(email, password, firstName, lastName); } else { await login(email, password); } router.push("/dashboard"); } catch (err: unknown) { if (err instanceof Error) { setError(err.message); } else { setError("Something went wrong. Please try again."); } } finally { setLoading(false); } } if (authLoading) { return (
{isRegister ? "Already have an account? " : "Don't have an account? "}