From c07d3198195d46ba2e84ff511ae44d5139d5aa97 Mon Sep 17 00:00:00 2001 From: iamkiddy Date: Thu, 4 Dec 2025 20:24:11 +0000 Subject: [PATCH] Enhance LoginDialog component to support optional redirect skipping. Update authentication flow to conditionally redirect users based on their role and current page, improving user experience during login on the booking page. --- app/(pages)/book-now/page.tsx | 1 + components/LoginDialog.tsx | 19 ++++++++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/app/(pages)/book-now/page.tsx b/app/(pages)/book-now/page.tsx index 73776cc..e73d349 100644 --- a/app/(pages)/book-now/page.tsx +++ b/app/(pages)/book-now/page.tsx @@ -930,6 +930,7 @@ export default function BookNowPage() { onLoginSuccess={handleLoginSuccess} onSwitchToSignup={handleSwitchToSignup} prefillEmail={loginPrefillEmail} + skipRedirect={true} /> {/* Signup Dialog */} diff --git a/components/LoginDialog.tsx b/components/LoginDialog.tsx index fa5fc3c..78237b4 100644 --- a/components/LoginDialog.tsx +++ b/components/LoginDialog.tsx @@ -15,7 +15,7 @@ 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 } from "next/navigation"; +import { useRouter, usePathname } from "next/navigation"; import { ForgotPasswordDialog } from "./ForgotPasswordDialog"; import { VerifyOtpDialog } from "./VerifyOtpDialog"; @@ -25,13 +25,15 @@ interface LoginDialogProps { onLoginSuccess: () => void; prefillEmail?: string; onSwitchToSignup?: () => void; + skipRedirect?: boolean; // Option to skip automatic redirect } // Login Dialog component -export function LoginDialog({ open, onOpenChange, onLoginSuccess, prefillEmail, onSwitchToSignup }: LoginDialogProps) { +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: "", @@ -89,11 +91,14 @@ export function LoginDialog({ open, onOpenChange, onLoginSuccess, prefillEmail, // Call onLoginSuccess callback first onLoginSuccess(); - // Redirect based on user role - const redirectPath = userIsAdmin ? "/admin/dashboard" : "/user/dashboard"; - setTimeout(() => { - window.location.href = redirectPath; - }, 200); + // 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.";