feat: Centralize authentication redirects to middleware, remove client-side redirect logic, and refine logout process.
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
import { useSession } from 'next-auth/react';
|
||||
import { useRouter, usePathname } from 'next/navigation';
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useEffect } from 'react';
|
||||
import { Footer } from '@/components/layout/Footer';
|
||||
import { CommonHeader } from '@/components/layout/CommonHeader';
|
||||
import { PresenceProvider } from '@/components/providers/presence-provider';
|
||||
@@ -27,16 +27,9 @@ export default function UserLayout({
|
||||
const { data: session, status } = useSession();
|
||||
const router = useRouter();
|
||||
const pathname = usePathname();
|
||||
const [hasTokens, setHasTokens] = useState(false);
|
||||
|
||||
const isDashboard = pathname === '/user/dashboard';
|
||||
const isPublic = isPublicPath(pathname);
|
||||
|
||||
// Fast check for existing tokens on mount
|
||||
useEffect(() => {
|
||||
setHasTokens(!!localStorage.getItem('accessToken'));
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (status === 'loading') return;
|
||||
|
||||
@@ -52,16 +45,11 @@ export default function UserLayout({
|
||||
return;
|
||||
}
|
||||
|
||||
// Protected pages require login
|
||||
if (!session) {
|
||||
// Check localStorage - if tokens exist, session might still be resolving
|
||||
// Don't redirect to /login prematurely (prevents login page flash)
|
||||
const tokensExist = !!localStorage.getItem('accessToken');
|
||||
if (!tokensExist) {
|
||||
router.replace('/login');
|
||||
}
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user