2025-11-06 12:34:29 +00:00
|
|
|
"use client";
|
|
|
|
|
|
2025-11-23 13:29:31 +00:00
|
|
|
import { useState, useEffect } from "react";
|
2025-11-06 12:34:29 +00:00
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
|
import { Input } from "@/components/ui/input";
|
2025-11-23 13:29:31 +00:00
|
|
|
import { Heart, Eye, EyeOff, X, Loader2 } from "lucide-react";
|
2025-11-06 12:34:29 +00:00
|
|
|
import Link from "next/link";
|
|
|
|
|
import Image from "next/image";
|
2025-11-23 13:29:31 +00:00
|
|
|
import { useRouter, useSearchParams } from "next/navigation";
|
2025-11-13 11:10:00 +00:00
|
|
|
import { useAppTheme } from "@/components/ThemeProvider";
|
2025-11-23 13:29:31 +00:00
|
|
|
import { useAuth } from "@/hooks/useAuth";
|
|
|
|
|
import { loginSchema, type LoginInput } from "@/lib/schema/auth";
|
|
|
|
|
import { toast } from "sonner";
|
2025-11-06 12:34:29 +00:00
|
|
|
|
|
|
|
|
export default function Login() {
|
2025-11-13 11:10:00 +00:00
|
|
|
const { theme } = useAppTheme();
|
|
|
|
|
const isDark = theme === "dark";
|
2025-11-06 12:34:29 +00:00
|
|
|
const [showPassword, setShowPassword] = useState(false);
|
|
|
|
|
const [rememberMe, setRememberMe] = useState(false);
|
2025-11-23 13:29:31 +00:00
|
|
|
const [formData, setFormData] = useState<LoginInput>({
|
|
|
|
|
email: "",
|
|
|
|
|
password: "",
|
|
|
|
|
});
|
|
|
|
|
const [errors, setErrors] = useState<Partial<Record<keyof LoginInput, string>>>({});
|
2025-11-06 12:34:29 +00:00
|
|
|
const router = useRouter();
|
2025-11-23 13:29:31 +00:00
|
|
|
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<Record<keyof LoginInput, string>> = {};
|
|
|
|
|
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 }));
|
|
|
|
|
}
|
|
|
|
|
};
|
2025-11-06 12:34:29 +00:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="min-h-screen relative flex items-center justify-center px-4 py-12">
|
|
|
|
|
{/* Background Image */}
|
|
|
|
|
<div className="absolute inset-0 z-0">
|
|
|
|
|
<Image
|
2025-11-22 20:18:52 +00:00
|
|
|
src="/woman.jpg"
|
2025-11-21 23:38:16 +00:00
|
|
|
alt="Therapy and counseling session with African American clients"
|
2025-11-06 12:34:29 +00:00
|
|
|
fill
|
|
|
|
|
className="object-cover object-center"
|
|
|
|
|
priority
|
|
|
|
|
sizes="100vw"
|
|
|
|
|
/>
|
|
|
|
|
{/* Overlay for better readability */}
|
2025-11-22 00:51:29 +00:00
|
|
|
<div className="absolute inset-0 bg-black/20"></div>
|
2025-11-06 12:34:29 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* Branding - Top Left */}
|
2025-11-22 00:51:29 +00:00
|
|
|
<div className="absolute top-8 left-8 flex items-center gap-3 z-30">
|
|
|
|
|
<Heart className="w-6 h-6 text-white" fill="white" />
|
|
|
|
|
<span className="text-white text-xl font-semibold">Attune Heart Therapy</span>
|
2025-11-06 12:34:29 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{/* Centered White Card - Login Form */}
|
2025-11-13 11:10:00 +00:00
|
|
|
<div className={`relative z-20 w-full max-w-md rounded-2xl shadow-2xl p-8 ${isDark ? 'bg-gray-800 border border-gray-700' : 'bg-white'}`}>
|
|
|
|
|
{/* Header with Close Button */}
|
|
|
|
|
<div className="flex items-start justify-between mb-2">
|
|
|
|
|
<div className="flex-1">
|
|
|
|
|
{/* Heading */}
|
|
|
|
|
<h1 className="text-3xl font-bold bg-gradient-to-r from-rose-600 via-pink-600 to-rose-600 bg-clip-text text-transparent mb-2">
|
|
|
|
|
Welcome back
|
|
|
|
|
</h1>
|
|
|
|
|
{/* Sign Up Prompt */}
|
|
|
|
|
<p className={`mb-6 ${isDark ? 'text-gray-400' : 'text-gray-600'}`}>
|
|
|
|
|
New to Attune Heart Therapy?{" "}
|
|
|
|
|
<Link href="/signup" className={`underline font-medium ${isDark ? 'text-blue-400 hover:text-blue-300' : 'text-blue-600 hover:text-blue-700'}`}>
|
|
|
|
|
Sign up
|
|
|
|
|
</Link>
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
{/* Close Button */}
|
|
|
|
|
<Button
|
|
|
|
|
onClick={() => router.back()}
|
|
|
|
|
variant="ghost"
|
|
|
|
|
size="icon"
|
|
|
|
|
className={`flex-shrink-0 w-8 h-8 rounded-full ${isDark ? 'text-gray-400 hover:text-gray-300 hover:bg-gray-700' : 'text-gray-500 hover:text-gray-700 hover:bg-gray-100'}`}
|
|
|
|
|
aria-label="Close"
|
|
|
|
|
>
|
|
|
|
|
<X className="w-5 h-5" />
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
2025-11-06 12:34:29 +00:00
|
|
|
|
|
|
|
|
{/* Login Form */}
|
2025-11-23 13:29:31 +00:00
|
|
|
<form className="space-y-6" onSubmit={handleSubmit}>
|
2025-11-06 12:34:29 +00:00
|
|
|
{/* Email Field */}
|
|
|
|
|
<div className="space-y-2">
|
2025-11-13 11:10:00 +00:00
|
|
|
<label htmlFor="email" className={`text-sm font-medium ${isDark ? 'text-gray-300' : 'text-black'}`}>
|
2025-11-06 12:34:29 +00:00
|
|
|
Email address
|
|
|
|
|
</label>
|
|
|
|
|
<Input
|
|
|
|
|
id="email"
|
|
|
|
|
type="email"
|
|
|
|
|
placeholder="Email address"
|
2025-11-23 13:29:31 +00:00
|
|
|
value={formData.email}
|
|
|
|
|
onChange={(e) => 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' : ''}`}
|
2025-11-06 12:34:29 +00:00
|
|
|
required
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* Password Field */}
|
|
|
|
|
<div className="space-y-2">
|
2025-11-13 11:10:00 +00:00
|
|
|
<label htmlFor="password" className={`text-sm font-medium ${isDark ? 'text-gray-300' : 'text-black'}`}>
|
2025-11-06 12:34:29 +00:00
|
|
|
Your password
|
|
|
|
|
</label>
|
|
|
|
|
<div className="relative">
|
|
|
|
|
<Input
|
|
|
|
|
id="password"
|
|
|
|
|
type={showPassword ? "text" : "password"}
|
|
|
|
|
placeholder="Your password"
|
2025-11-23 13:29:31 +00:00
|
|
|
value={formData.password}
|
|
|
|
|
onChange={(e) => 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' : ''}`}
|
2025-11-06 12:34:29 +00:00
|
|
|
required
|
|
|
|
|
/>
|
|
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
variant="ghost"
|
|
|
|
|
size="icon"
|
|
|
|
|
onClick={() => setShowPassword(!showPassword)}
|
2025-11-13 11:10:00 +00:00
|
|
|
className={`absolute right-4 top-1/2 -translate-y-1/2 h-auto w-auto p-0 ${isDark ? 'text-gray-400 hover:text-gray-300' : 'text-gray-500 hover:text-gray-700'}`}
|
2025-11-06 12:34:29 +00:00
|
|
|
aria-label={showPassword ? "Hide password" : "Show password"}
|
|
|
|
|
>
|
|
|
|
|
{showPassword ? (
|
|
|
|
|
<EyeOff className="w-5 h-5" />
|
|
|
|
|
) : (
|
|
|
|
|
<Eye className="w-5 h-5" />
|
|
|
|
|
)}
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
2025-11-23 13:29:31 +00:00
|
|
|
{errors.password && (
|
|
|
|
|
<p className="text-sm text-red-500">{errors.password}</p>
|
|
|
|
|
)}
|
2025-11-06 12:34:29 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* Submit Button */}
|
|
|
|
|
<Button
|
|
|
|
|
type="submit"
|
2025-11-23 13:29:31 +00:00
|
|
|
disabled={loginMutation.isPending}
|
|
|
|
|
className="w-full h-12 text-base font-semibold bg-gradient-to-r from-rose-500 to-pink-600 hover:from-rose-600 hover:to-pink-700 text-white shadow-lg hover:shadow-xl transition-all disabled:opacity-50 disabled:cursor-not-allowed"
|
2025-11-06 12:34:29 +00:00
|
|
|
>
|
2025-11-23 13:29:31 +00:00
|
|
|
{loginMutation.isPending ? (
|
|
|
|
|
<>
|
|
|
|
|
<Loader2 className="w-4 h-4 mr-2 animate-spin" />
|
|
|
|
|
Logging in...
|
|
|
|
|
</>
|
|
|
|
|
) : (
|
|
|
|
|
"Log in"
|
|
|
|
|
)}
|
2025-11-06 12:34:29 +00:00
|
|
|
</Button>
|
|
|
|
|
|
|
|
|
|
{/* Remember Me & Forgot Password */}
|
|
|
|
|
<div className="flex items-center justify-between text-sm">
|
|
|
|
|
<label className="flex items-center gap-2 cursor-pointer">
|
|
|
|
|
<input
|
|
|
|
|
type="checkbox"
|
|
|
|
|
checked={rememberMe}
|
|
|
|
|
onChange={(e) => setRememberMe(e.target.checked)}
|
2025-11-13 11:10:00 +00:00
|
|
|
className={`w-4 h-4 rounded text-rose-600 focus:ring-2 focus:ring-rose-500 cursor-pointer ${isDark ? 'border-gray-600 bg-gray-700' : 'border-gray-300'}`}
|
2025-11-06 12:34:29 +00:00
|
|
|
/>
|
2025-11-13 11:10:00 +00:00
|
|
|
<span className={isDark ? 'text-gray-300' : 'text-black'}>Remember me</span>
|
2025-11-06 12:34:29 +00:00
|
|
|
</label>
|
|
|
|
|
<Link
|
|
|
|
|
href="/forgot-password"
|
2025-11-13 11:10:00 +00:00
|
|
|
className={`font-medium ${isDark ? 'text-blue-400 hover:text-blue-300' : 'text-blue-600 hover:text-blue-700'}`}
|
2025-11-06 12:34:29 +00:00
|
|
|
>
|
|
|
|
|
Forgot password?
|
|
|
|
|
</Link>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
</form>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|