"use client"; import { useState, useEffect } from "react"; import { Button } from "@/components/ui/button"; import { useAppTheme } from "@/components/ThemeProvider"; import { Input } from "@/components/ui/input"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Eye, EyeOff, Loader2, X, Mail } from "lucide-react"; import { useAuth } from "@/hooks/useAuth"; import { loginSchema, type LoginInput } from "@/lib/schema/auth"; import { toast } from "sonner"; import { useRouter, usePathname } from "next/navigation"; import { ForgotPasswordDialog } from "./ForgotPasswordDialog"; import { VerifyOtpDialog } from "./VerifyOtpDialog"; interface LoginDialogProps { open: boolean; onOpenChange: (open: boolean) => void; onLoginSuccess: () => void; prefillEmail?: string; onSwitchToSignup?: () => void; skipRedirect?: boolean; // Option to skip automatic redirect } // Login Dialog component export function LoginDialog({ open, onOpenChange, onLoginSuccess, prefillEmail, onSwitchToSignup, skipRedirect = false }: LoginDialogProps) { const { theme } = useAppTheme(); const isDark = theme === "dark"; const router = useRouter(); const pathname = usePathname(); const { login, loginMutation } = useAuth(); const [loginData, setLoginData] = useState({ email: "", password: "", }); const [showPassword, setShowPassword] = useState(false); const [forgotPasswordDialogOpen, setForgotPasswordDialogOpen] = useState(false); const [showResendOtp, setShowResendOtp] = useState(false); const [verifyOtpDialogOpen, setVerifyOtpDialogOpen] = useState(false); // Pre-fill email if provided useEffect(() => { if (prefillEmail && open) { setLoginData(prev => ({ ...prev, email: prefillEmail })); } }, [prefillEmail, open]); const handleLogin = async (e: React.FormEvent) => { e.preventDefault(); // Validate form const validation = loginSchema.safeParse(loginData); if (!validation.success) { const firstError = validation.error.issues[0]; toast.error(firstError.message); return; } try { const result = await login(loginData); if (result.tokens && result.user) { toast.success("Login successful!"); setShowPassword(false); setShowResendOtp(false); onOpenChange(false); // Reset form setLoginData({ email: "", password: "" }); // Check if user is admin/staff/superuser const user = result.user as any; const isTruthy = (value: any): boolean => { if (value === true || value === "true" || value === 1 || value === "1") return true; return false; }; const userIsAdmin = isTruthy(user.is_admin) || isTruthy(user.isAdmin) || isTruthy(user.is_staff) || isTruthy(user.isStaff) || isTruthy(user.is_superuser) || isTruthy(user.isSuperuser); // Call onLoginSuccess callback first onLoginSuccess(); // Only redirect if skipRedirect is false and we're not on the booking page if (!skipRedirect && pathname !== "/book-now") { // Redirect based on user role const redirectPath = userIsAdmin ? "/admin/dashboard" : "/user/dashboard"; setTimeout(() => { window.location.href = redirectPath; }, 200); } } } catch (err) { const errorMessage = err instanceof Error ? err.message : "Login failed. Please try again."; toast.error(errorMessage); // Check if error is about email verification if (errorMessage.toLowerCase().includes("verify your email") || errorMessage.toLowerCase().includes("email address before logging")) { setShowResendOtp(true); } else { setShowResendOtp(false); } } }; // Handle resend OTP - just open the verification dialog (it will auto-send OTP) const handleResendOtp = () => { if (!loginData.email) { toast.error("Email address is required to resend OTP."); return; } // Close login dialog and open OTP verification dialog // The VerifyOtpDialog will automatically send the OTP when it opens setShowResendOtp(false); onOpenChange(false); setTimeout(() => { setVerifyOtpDialogOpen(true); }, 100); }; // Handle OTP verification success const handleOtpVerificationSuccess = () => { // After successful verification, user can try logging in again setVerifyOtpDialogOpen(false); // Optionally reopen login dialog setTimeout(() => { onOpenChange(true); }, 100); }; // Reset form when dialog closes const handleDialogChange = (isOpen: boolean) => { if (!isOpen) { setLoginData({ email: "", password: "" }); setShowResendOtp(false); } onOpenChange(isOpen); }; return ( {/* Header with Close Button - Fixed */}
Welcome back Please log in to complete your booking {/* Close Button */}
{/* Scrollable Content */}
{/* Login Form */}
{/* Email Field */}
setLoginData({ ...loginData, email: e.target.value })} className={`h-11 sm:h-12 text-sm sm:text-base ${isDark ? 'bg-gray-700 border-gray-600 text-white placeholder:text-gray-400' : 'bg-white border-gray-300 text-gray-900'}`} required />
{/* Password Field */}
setLoginData({ ...loginData, password: e.target.value })} className={`h-11 sm:h-12 pr-12 text-sm sm:text-base ${isDark ? 'bg-gray-700 border-gray-600 text-white placeholder:text-gray-400' : 'bg-white border-gray-300 text-gray-900'}`} required />
{/* Submit Button */} {/* Resend OTP - Show when email verification error occurs */} {showResendOtp && (

Email verification required

Please verify your email address before logging in. We can resend the verification code to {loginData.email}.

)} {/* Forgot Password */}
{/* Sign Up Prompt */}

New to Attune Heart Therapy?{" "}

{/* Forgot Password Dialog */} {/* Verify OTP Dialog */}
); }