"use client"; import { useState, useEffect } from "react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Heart, Eye, EyeOff, X, Loader2 } from "lucide-react"; import Link from "next/link"; import Image from "next/image"; import { useRouter, useSearchParams } from "next/navigation"; import { useAppTheme } from "@/components/ThemeProvider"; import { useAuth } from "@/hooks/useAuth"; import { loginSchema, type LoginInput } from "@/lib/schema/auth"; import { toast } from "sonner"; export default function Login() { const { theme } = useAppTheme(); const isDark = theme === "dark"; const [showPassword, setShowPassword] = useState(false); const [rememberMe, setRememberMe] = useState(false); const [formData, setFormData] = useState({ email: "", password: "", }); const [errors, setErrors] = useState>>({}); const router = useRouter(); const searchParams = useSearchParams(); const { login, isAuthenticated, loginMutation } = useAuth(); // Redirect if already authenticated useEffect(() => { if (isAuthenticated) { const redirect = searchParams.get("redirect") || "/admin/dashboard"; router.push(redirect); } }, [isAuthenticated, router, searchParams]); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setErrors({}); // Validate form const validation = loginSchema.safeParse(formData); if (!validation.success) { const fieldErrors: Partial> = {}; validation.error.issues.forEach((err) => { if (err.path[0]) { fieldErrors[err.path[0] as keyof LoginInput] = err.message; } }); setErrors(fieldErrors); return; } try { const result = await login(formData); if (result.tokens && result.user) { toast.success("Login successful!"); // Normalize user data const user = result.user; // Check for admin status - check multiple possible field names const isAdmin = user.is_admin === true || (user as any)?.isAdmin === true || (user as any)?.is_staff === true || (user as any)?.isStaff === true; // Redirect based on user role const redirect = searchParams.get("redirect"); if (redirect) { router.push(redirect); } else { // Default to admin dashboard router.push("/admin/dashboard"); } } } catch (error) { const errorMessage = error instanceof Error ? error.message : "Login failed. Please try again."; toast.error(errorMessage); // Don't set field errors for server errors, only show toast setErrors({}); } }; const handleChange = (field: keyof LoginInput, value: string) => { setFormData((prev) => ({ ...prev, [field]: value })); // Clear error when user starts typing if (errors[field]) { setErrors((prev) => ({ ...prev, [field]: undefined })); } }; return (
{/* Background Image */}
Therapy and counseling session with African American clients {/* Overlay for better readability */}
{/* Branding - Top Left */}
Attune Heart Therapy
{/* Centered White Card - Login Form */}
{/* Header with Close Button */}
{/* Heading */}

Welcome back

{/* Sign Up Prompt */}

New to Attune Heart Therapy?{" "} Sign up

{/* Close Button */}
{/* Login Form */}
{/* Email Field */}
handleChange("email", e.target.value)} className={`h-12 ${isDark ? 'bg-gray-700 border-gray-600 text-white placeholder:text-gray-400' : 'bg-white border-gray-300 text-gray-900'} ${errors.email ? 'border-red-500' : ''}`} required />
{/* Password Field */}
handleChange("password", e.target.value)} className={`h-12 pr-12 ${isDark ? 'bg-gray-700 border-gray-600 text-white placeholder:text-gray-400' : 'bg-white border-gray-300 text-gray-900'} ${errors.password ? 'border-red-500' : ''}`} required />
{errors.password && (

{errors.password}

)}
{/* Submit Button */} {/* Remember Me & Forgot Password */}
Forgot password?
); }