"use client" import { useState, useEffect } from "react" import { useRouter } from "next/navigation" import { Eye, EyeOff, AlertCircle } from "lucide-react" export default function ClientLoginPage() { const router = useRouter() const [email, setEmail] = useState("") const [password, setPassword] = useState("") const [showPassword, setShowPassword] = useState(false) const [loading, setLoading] = useState(false) const [error, setError] = useState("") const [checking, setChecking] = useState(true) const [debugInfo, setDebugInfo] = useState("") useEffect(() => { fetch("/api/auth/me", { credentials: "include" }) .then((r) => r.json()) .then((data) => { if (data.user?.role === "client") router.push("/client-portal/dashboard") else setChecking(false) }) .catch(() => setChecking(false)) }, [router]) const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setLoading(true) setError("") setDebugInfo("") try { const res = await fetch("/api/auth/login", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ email, password }), }) const data = await res.json() console.log("LOGIN RESPONSE:", res.status, data) if (!res.ok) { setError(data.error || "Login failed") setDebugInfo(`HTTP ${res.status}`) return } if (data.user?.role !== "client") { setError("This portal is for clients only") setDebugInfo(`User role: ${data.user?.role}`) return } setError("Login successful! Redirecting...") setDebugInfo("") setTimeout(() => { window.location.href = "/client-portal/dashboard" }, 300) } catch (err) { console.error("LOGIN FETCH ERROR:", err) setError("Connection error") setDebugInfo("Check network tab for details") } finally { setLoading(false) } } if (checking) { return (
Sign in to view your projects and invoices