2025-11-07 20:19:08 +00:00
|
|
|
"use client";
|
|
|
|
|
|
2025-11-24 22:35:07 +00:00
|
|
|
import { useState, useEffect } from "react";
|
2025-11-07 20:19:08 +00:00
|
|
|
import { Button } from "@/components/ui/button";
|
2025-11-13 11:10:00 +00:00
|
|
|
import { useAppTheme } from "@/components/ThemeProvider";
|
2025-11-07 20:19:08 +00:00
|
|
|
import { Input } from "@/components/ui/input";
|
|
|
|
|
import {
|
|
|
|
|
Dialog,
|
|
|
|
|
DialogContent,
|
|
|
|
|
DialogDescription,
|
|
|
|
|
DialogHeader,
|
|
|
|
|
DialogTitle,
|
|
|
|
|
} from "@/components/ui/dialog";
|
2025-11-25 20:15:37 +00:00
|
|
|
import { Eye, EyeOff, Loader2, X, Mail } from "lucide-react";
|
2025-11-23 13:29:31 +00:00
|
|
|
import { useAuth } from "@/hooks/useAuth";
|
2025-11-24 22:35:07 +00:00
|
|
|
import { loginSchema, type LoginInput } from "@/lib/schema/auth";
|
2025-11-23 13:29:31 +00:00
|
|
|
import { toast } from "sonner";
|
2025-12-04 20:24:11 +00:00
|
|
|
import { useRouter, usePathname } from "next/navigation";
|
2025-11-24 22:35:07 +00:00
|
|
|
import { ForgotPasswordDialog } from "./ForgotPasswordDialog";
|
2025-11-25 20:15:37 +00:00
|
|
|
import { VerifyOtpDialog } from "./VerifyOtpDialog";
|
2025-11-07 20:19:08 +00:00
|
|
|
|
|
|
|
|
interface LoginDialogProps {
|
|
|
|
|
open: boolean;
|
|
|
|
|
onOpenChange: (open: boolean) => void;
|
|
|
|
|
onLoginSuccess: () => void;
|
2025-11-24 22:35:07 +00:00
|
|
|
prefillEmail?: string;
|
|
|
|
|
onSwitchToSignup?: () => void;
|
2025-12-04 20:24:11 +00:00
|
|
|
skipRedirect?: boolean; // Option to skip automatic redirect
|
2025-11-07 20:19:08 +00:00
|
|
|
}
|
|
|
|
|
|
2025-11-07 20:28:22 +00:00
|
|
|
// Login Dialog component
|
2025-12-04 20:24:11 +00:00
|
|
|
export function LoginDialog({ open, onOpenChange, onLoginSuccess, prefillEmail, onSwitchToSignup, skipRedirect = false }: LoginDialogProps) {
|
2025-11-13 11:10:00 +00:00
|
|
|
const { theme } = useAppTheme();
|
|
|
|
|
const isDark = theme === "dark";
|
2025-11-23 13:29:31 +00:00
|
|
|
const router = useRouter();
|
2025-12-04 20:24:11 +00:00
|
|
|
const pathname = usePathname();
|
2025-11-24 22:35:07 +00:00
|
|
|
const { login, loginMutation } = useAuth();
|
2025-11-23 13:29:31 +00:00
|
|
|
const [loginData, setLoginData] = useState<LoginInput>({
|
2025-11-07 20:19:08 +00:00
|
|
|
email: "",
|
|
|
|
|
password: "",
|
|
|
|
|
});
|
|
|
|
|
const [showPassword, setShowPassword] = useState(false);
|
2025-11-24 22:35:07 +00:00
|
|
|
const [forgotPasswordDialogOpen, setForgotPasswordDialogOpen] = useState(false);
|
2025-11-25 20:15:37 +00:00
|
|
|
const [showResendOtp, setShowResendOtp] = useState(false);
|
|
|
|
|
const [verifyOtpDialogOpen, setVerifyOtpDialogOpen] = useState(false);
|
2025-11-24 22:35:07 +00:00
|
|
|
|
|
|
|
|
// Pre-fill email if provided
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (prefillEmail && open) {
|
|
|
|
|
setLoginData(prev => ({ ...prev, email: prefillEmail }));
|
|
|
|
|
}
|
|
|
|
|
}, [prefillEmail, open]);
|
2025-11-07 20:19:08 +00:00
|
|
|
|
|
|
|
|
const handleLogin = async (e: React.FormEvent) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
2025-11-23 13:29:31 +00:00
|
|
|
// Validate form
|
|
|
|
|
const validation = loginSchema.safeParse(loginData);
|
|
|
|
|
if (!validation.success) {
|
2025-11-24 16:38:09 +00:00
|
|
|
const firstError = validation.error.issues[0];
|
2025-11-24 18:26:37 +00:00
|
|
|
toast.error(firstError.message);
|
2025-11-23 13:29:31 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-07 20:19:08 +00:00
|
|
|
try {
|
2025-11-23 13:29:31 +00:00
|
|
|
const result = await login(loginData);
|
2025-11-07 20:19:08 +00:00
|
|
|
|
2025-11-23 13:29:31 +00:00
|
|
|
if (result.tokens && result.user) {
|
|
|
|
|
toast.success("Login successful!");
|
2025-11-24 22:35:07 +00:00
|
|
|
setShowPassword(false);
|
2025-11-25 20:15:37 +00:00
|
|
|
setShowResendOtp(false);
|
2025-11-24 22:35:07 +00:00
|
|
|
onOpenChange(false);
|
|
|
|
|
// Reset form
|
|
|
|
|
setLoginData({ email: "", password: "" });
|
2025-11-25 21:25:53 +00:00
|
|
|
|
|
|
|
|
// 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
|
2025-11-24 22:35:07 +00:00
|
|
|
onLoginSuccess();
|
2025-11-25 21:25:53 +00:00
|
|
|
|
2025-12-04 20:24:11 +00:00
|
|
|
// Only redirect if skipRedirect is false and we're not on the booking page
|
|
|
|
|
if (!skipRedirect && pathname !== "/book-now") {
|
|
|
|
|
// Redirect based on user role
|
2025-12-05 18:00:54 +00:00
|
|
|
const redirectPath = userIsAdmin ? "/admin/booking" : "/user/dashboard";
|
2025-12-04 20:24:11 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
|
window.location.href = redirectPath;
|
|
|
|
|
}, 200);
|
|
|
|
|
}
|
2025-11-23 13:29:31 +00:00
|
|
|
}
|
2025-11-07 20:19:08 +00:00
|
|
|
} catch (err) {
|
2025-11-23 13:29:31 +00:00
|
|
|
const errorMessage = err instanceof Error ? err.message : "Login failed. Please try again.";
|
|
|
|
|
toast.error(errorMessage);
|
2025-11-25 20:15:37 +00:00
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
}
|
2025-11-07 20:19:08 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-11-25 20:15:37 +00:00
|
|
|
// 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);
|
|
|
|
|
};
|
|
|
|
|
|
2025-11-24 22:35:07 +00:00
|
|
|
// Reset form when dialog closes
|
|
|
|
|
const handleDialogChange = (isOpen: boolean) => {
|
|
|
|
|
if (!isOpen) {
|
|
|
|
|
setLoginData({ email: "", password: "" });
|
2025-11-25 20:15:37 +00:00
|
|
|
setShowResendOtp(false);
|
2025-11-07 20:28:22 +00:00
|
|
|
}
|
2025-11-24 22:35:07 +00:00
|
|
|
onOpenChange(isOpen);
|
2025-11-07 20:28:22 +00:00
|
|
|
};
|
|
|
|
|
|
2025-11-07 20:19:08 +00:00
|
|
|
return (
|
2025-11-24 22:35:07 +00:00
|
|
|
<Dialog open={open} onOpenChange={handleDialogChange}>
|
2025-11-13 11:10:00 +00:00
|
|
|
<DialogContent
|
|
|
|
|
showCloseButton={false}
|
2025-11-24 16:21:21 +00:00
|
|
|
className={`max-w-md max-h-[90vh] overflow-hidden flex flex-col p-0 ${isDark ? 'bg-gray-800 border-gray-700' : 'bg-white border-gray-200'}`}
|
2025-11-13 11:10:00 +00:00
|
|
|
>
|
2025-11-24 16:21:21 +00:00
|
|
|
{/* Header with Close Button - Fixed */}
|
|
|
|
|
<div className="flex items-start justify-between p-6 pb-4 flex-shrink-0 border-b border-gray-200 dark:border-gray-700">
|
|
|
|
|
<DialogHeader className="flex-1 pr-2">
|
|
|
|
|
<DialogTitle className="text-2xl sm:text-3xl font-bold bg-gradient-to-r from-rose-600 via-pink-600 to-rose-600 bg-clip-text text-transparent">
|
2025-11-24 22:35:07 +00:00
|
|
|
Welcome back
|
2025-11-13 11:10:00 +00:00
|
|
|
</DialogTitle>
|
2025-11-24 16:21:21 +00:00
|
|
|
<DialogDescription className={`text-sm sm:text-base mt-1 ${isDark ? 'text-gray-400' : 'text-gray-600'}`}>
|
2025-11-24 22:35:07 +00:00
|
|
|
Please log in to complete your booking
|
2025-11-13 11:10:00 +00:00
|
|
|
</DialogDescription>
|
|
|
|
|
</DialogHeader>
|
|
|
|
|
{/* Close Button */}
|
2025-11-24 22:35:07 +00:00
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
variant="ghost"
|
|
|
|
|
size="icon"
|
|
|
|
|
onClick={() => handleDialogChange(false)}
|
|
|
|
|
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'}`}
|
2025-11-13 11:10:00 +00:00
|
|
|
aria-label="Close"
|
|
|
|
|
>
|
|
|
|
|
<X className="w-5 h-5" />
|
2025-11-24 22:35:07 +00:00
|
|
|
</Button>
|
2025-11-13 11:10:00 +00:00
|
|
|
</div>
|
2025-11-07 20:19:08 +00:00
|
|
|
|
2025-11-24 16:21:21 +00:00
|
|
|
{/* Scrollable Content */}
|
|
|
|
|
<div className="overflow-y-auto flex-1 px-6">
|
2025-11-24 22:35:07 +00:00
|
|
|
{/* Login Form */}
|
|
|
|
|
<form className="space-y-4 sm:space-y-5 py-4 sm:py-6" onSubmit={handleLogin}>
|
2025-11-07 20:28:22 +00:00
|
|
|
{/* Email Field */}
|
2025-11-24 16:21:21 +00:00
|
|
|
<div className="space-y-1.5 sm:space-y-2">
|
2025-11-24 22:35:07 +00:00
|
|
|
<label htmlFor="login-email" className={`text-sm font-medium ${isDark ? 'text-gray-300' : 'text-black'}`}>
|
|
|
|
|
Email address
|
2025-11-07 20:28:22 +00:00
|
|
|
</label>
|
|
|
|
|
<Input
|
2025-11-24 22:35:07 +00:00
|
|
|
id="login-email"
|
2025-11-07 20:28:22 +00:00
|
|
|
type="email"
|
|
|
|
|
placeholder="Email address"
|
2025-11-24 22:35:07 +00:00
|
|
|
value={loginData.email}
|
|
|
|
|
onChange={(e) => setLoginData({ ...loginData, email: e.target.value })}
|
2025-11-24 16:21:21 +00:00
|
|
|
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'}`}
|
2025-11-07 20:28:22 +00:00
|
|
|
required
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
|
2025-11-23 13:29:31 +00:00
|
|
|
{/* Password Field */}
|
2025-11-24 16:21:21 +00:00
|
|
|
<div className="space-y-1.5 sm:space-y-2">
|
2025-11-24 22:35:07 +00:00
|
|
|
<label htmlFor="login-password" className={`text-sm font-medium ${isDark ? 'text-gray-300' : 'text-black'}`}>
|
|
|
|
|
Your password
|
2025-11-23 13:29:31 +00:00
|
|
|
</label>
|
|
|
|
|
<div className="relative">
|
|
|
|
|
<Input
|
2025-11-24 22:35:07 +00:00
|
|
|
id="login-password"
|
2025-11-23 13:29:31 +00:00
|
|
|
type={showPassword ? "text" : "password"}
|
2025-11-24 22:35:07 +00:00
|
|
|
placeholder="Your password"
|
|
|
|
|
value={loginData.password}
|
|
|
|
|
onChange={(e) => setLoginData({ ...loginData, password: e.target.value })}
|
2025-11-24 16:21:21 +00:00
|
|
|
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'}`}
|
2025-11-23 13:29:31 +00:00
|
|
|
required
|
|
|
|
|
/>
|
|
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
variant="ghost"
|
|
|
|
|
size="icon"
|
|
|
|
|
onClick={() => setShowPassword(!showPassword)}
|
2025-11-24 16:21:21 +00:00
|
|
|
className={`absolute right-3 sm: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-23 13:29:31 +00:00
|
|
|
aria-label={showPassword ? "Hide password" : "Show password"}
|
|
|
|
|
>
|
|
|
|
|
{showPassword ? (
|
2025-11-24 16:21:21 +00:00
|
|
|
<EyeOff className="w-4 h-4 sm:w-5 sm:h-5" />
|
2025-11-23 13:29:31 +00:00
|
|
|
) : (
|
2025-11-24 16:21:21 +00:00
|
|
|
<Eye className="w-4 h-4 sm:w-5 sm:h-5" />
|
2025-11-23 13:29:31 +00:00
|
|
|
)}
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
2025-11-07 20:28:22 +00:00
|
|
|
{/* Submit Button */}
|
|
|
|
|
<Button
|
|
|
|
|
type="submit"
|
2025-11-24 22:35:07 +00:00
|
|
|
disabled={loginMutation.isPending}
|
2025-11-24 16:21:21 +00:00
|
|
|
className="w-full h-11 sm:h-12 text-sm sm: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 mt-4 sm:mt-6"
|
2025-11-07 20:28:22 +00:00
|
|
|
>
|
2025-11-24 22:35:07 +00:00
|
|
|
{loginMutation.isPending ? (
|
2025-11-07 20:28:22 +00:00
|
|
|
<>
|
|
|
|
|
<Loader2 className="w-4 h-4 mr-2 animate-spin" />
|
2025-11-24 22:35:07 +00:00
|
|
|
Logging in...
|
2025-11-07 20:28:22 +00:00
|
|
|
</>
|
|
|
|
|
) : (
|
2025-11-24 22:35:07 +00:00
|
|
|
"Log in"
|
2025-11-07 20:28:22 +00:00
|
|
|
)}
|
|
|
|
|
</Button>
|
|
|
|
|
|
2025-11-25 20:15:37 +00:00
|
|
|
{/* Resend OTP - Show when email verification error occurs */}
|
|
|
|
|
{showResendOtp && (
|
|
|
|
|
<div className={`p-3 sm:p-4 rounded-lg border ${isDark ? 'bg-yellow-900/20 border-yellow-800' : 'bg-yellow-50 border-yellow-200'}`}>
|
|
|
|
|
<div className="flex items-start gap-3">
|
|
|
|
|
<Mail className={`w-5 h-5 mt-0.5 flex-shrink-0 ${isDark ? 'text-yellow-400' : 'text-yellow-600'}`} />
|
|
|
|
|
<div className="flex-1">
|
|
|
|
|
<p className={`text-sm font-medium ${isDark ? 'text-yellow-200' : 'text-yellow-900'}`}>
|
|
|
|
|
Email verification required
|
|
|
|
|
</p>
|
|
|
|
|
<p className={`text-xs sm:text-sm mt-1 ${isDark ? 'text-yellow-300' : 'text-yellow-700'}`}>
|
|
|
|
|
Please verify your email address before logging in. We can resend the verification code to {loginData.email}.
|
|
|
|
|
</p>
|
|
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
variant="link"
|
|
|
|
|
onClick={handleResendOtp}
|
|
|
|
|
className={`h-auto p-0 mt-2 text-xs sm:text-sm font-medium ${isDark ? 'text-yellow-400 hover:text-yellow-300' : 'text-yellow-700 hover:text-yellow-800'}`}
|
|
|
|
|
>
|
|
|
|
|
Resend verification code
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
2025-11-24 22:35:07 +00:00
|
|
|
{/* Forgot Password */}
|
|
|
|
|
<div className="flex items-center justify-end text-xs sm:text-sm">
|
|
|
|
|
<Button
|
2025-11-07 20:28:22 +00:00
|
|
|
type="button"
|
2025-11-24 22:35:07 +00:00
|
|
|
variant="link"
|
|
|
|
|
className={`font-medium text-xs sm:text-sm h-auto p-0 ${isDark ? 'text-blue-400 hover:text-blue-300' : 'text-blue-600 hover:text-blue-700'}`}
|
|
|
|
|
onClick={() => {
|
|
|
|
|
handleDialogChange(false);
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
setForgotPasswordDialogOpen(true);
|
|
|
|
|
}, 100);
|
|
|
|
|
}}
|
2025-11-07 20:28:22 +00:00
|
|
|
>
|
2025-11-24 22:35:07 +00:00
|
|
|
Forgot password?
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
2025-11-07 20:19:08 +00:00
|
|
|
|
2025-11-24 22:35:07 +00:00
|
|
|
{/* Sign Up Prompt */}
|
|
|
|
|
<p className={`text-xs sm:text-sm text-center pt-2 ${isDark ? 'text-gray-400' : 'text-gray-600'}`}>
|
|
|
|
|
New to Attune Heart Therapy?{" "}
|
2025-11-07 20:19:08 +00:00
|
|
|
<Button
|
|
|
|
|
type="button"
|
2025-11-24 22:35:07 +00:00
|
|
|
variant="link"
|
|
|
|
|
onClick={() => {
|
|
|
|
|
handleDialogChange(false);
|
|
|
|
|
if (onSwitchToSignup) {
|
|
|
|
|
onSwitchToSignup();
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
className={`h-auto p-0 font-medium ${isDark ? 'text-blue-400 hover:text-blue-300' : 'text-blue-600 hover:text-blue-700'}`}
|
2025-11-07 20:19:08 +00:00
|
|
|
>
|
2025-11-24 22:35:07 +00:00
|
|
|
Sign up
|
2025-11-07 20:19:08 +00:00
|
|
|
</Button>
|
2025-11-24 22:35:07 +00:00
|
|
|
</p>
|
|
|
|
|
</form>
|
2025-11-24 16:21:21 +00:00
|
|
|
</div>
|
2025-11-07 20:19:08 +00:00
|
|
|
</DialogContent>
|
2025-11-24 22:35:07 +00:00
|
|
|
|
|
|
|
|
{/* Forgot Password Dialog */}
|
|
|
|
|
<ForgotPasswordDialog
|
|
|
|
|
open={forgotPasswordDialogOpen}
|
|
|
|
|
onOpenChange={setForgotPasswordDialogOpen}
|
|
|
|
|
/>
|
2025-11-25 20:15:37 +00:00
|
|
|
|
|
|
|
|
{/* Verify OTP Dialog */}
|
|
|
|
|
<VerifyOtpDialog
|
|
|
|
|
open={verifyOtpDialogOpen}
|
|
|
|
|
onOpenChange={setVerifyOtpDialogOpen}
|
|
|
|
|
email={loginData.email}
|
|
|
|
|
context="registration"
|
|
|
|
|
onVerificationSuccess={handleOtpVerificationSuccess}
|
|
|
|
|
/>
|
2025-11-07 20:19:08 +00:00
|
|
|
</Dialog>
|
|
|
|
|
);
|
|
|
|
|
}
|