'use client'; import { useSession } from 'next-auth/react'; import { useRouter, usePathname } from 'next/navigation'; import { useEffect } from 'react'; import Image from 'next/image'; import { Footer } from '@/components/layout/Footer'; import { CommonHeader } from '@/components/layout/CommonHeader'; import { PresenceProvider } from '@/components/providers/presence-provider'; // Pages that don't require authentication const publicPaths = [ '/user/dashboard', '/user/profiles', '/user/profile/', // Agent profile view (includes /user/profile/[id]) ]; // Check if current path is public const isPublicPath = (pathname: string) => { return publicPaths.some(path => pathname === path || pathname.startsWith(path)); }; export default function UserLayout({ children, }: { children: React.ReactNode; }) { const { data: session, status } = useSession(); const router = useRouter(); const pathname = usePathname(); const isDashboard = pathname === '/user/dashboard'; const isPublic = isPublicPath(pathname); useEffect(() => { if (status === 'loading') return; // Allow public pages without authentication if (isPublic) { // If logged in as agent, redirect to agent dashboard only from user dashboard if (session && pathname === '/user/dashboard') { const userRole = (session.user as any)?.role; if (userRole === 'AGENT') { router.replace('/agent/dashboard'); } } return; } // Don't redirect to /login here for protected pages — the middleware handles that. // Doing it here causes race conditions during logout (layout detects session loss // and redirects to /login via client-side navigation, conflicting with server-side // logout redirect). if (!session) return; // Redirect agents to agent dashboard for protected user pages const userRole = (session.user as any)?.role; if (userRole === 'AGENT') { router.replace('/agent/dashboard'); } }, [session, status, router, pathname, isPublic]); const splashLoading = (
RE-Quest
); // Show loading only for protected pages while checking auth if (status === 'loading' && !isPublic) { return splashLoading; } // Redirect protected pages if not logged in if (!session && !isPublic) { return splashLoading; } return (
{isDashboard ? ( <> {/* Main Content with Header Overlay for Dashboard */}
{/* Header overlaying the content */}
{children}
) : ( <> {/* Regular Header for other pages */}
{/* Main Content */}
{children}
)} {/* Footer */}
); }