diff --git a/src/components/NavigationInitializer.tsx b/src/components/NavigationInitializer.tsx new file mode 100644 index 0000000..d1f7fd0 --- /dev/null +++ b/src/components/NavigationInitializer.tsx @@ -0,0 +1,17 @@ +import { useEffect } from 'react'; +import { useNavigate } from 'react-router-dom'; +import { setNavigate } from '@/utils/navigation'; + +/** + * Component to initialize navigation utility for use in services/interceptors + * This should be rendered once at the app level + */ +export const NavigationInitializer = (): null => { + const navigate = useNavigate(); + + useEffect(() => { + setNavigate(navigate); + }, [navigate]); + + return null; +}; diff --git a/src/components/layout/Header.tsx b/src/components/layout/Header.tsx index 44c2900..104fbda 100644 --- a/src/components/layout/Header.tsx +++ b/src/components/layout/Header.tsx @@ -69,14 +69,18 @@ export const Header = ({ breadcrumbs, currentPage, onMenuClick }: HeaderProps): // Close dropdown immediately setIsDropdownOpen(false); + // Check if user is on a tenant route to determine redirect path + const isTenantRoute = window.location.pathname.startsWith('/tenant'); + const redirectPath = isTenantRoute ? '/tenant/login' : '/'; + try { // Call logout API with Bearer token const result = await dispatch(logoutAsync()).unwrap(); const message = result.message || 'Logged out successfully'; const description = result.message ? undefined : 'You have been logged out'; showToast.success(message, description); - // Clear state and redirect - navigate('/', { replace: true }); + // Clear state and redirect to appropriate login page + navigate(redirectPath, { replace: true }); } catch (error: any) { // Even if API call fails, clear local state and redirect to login console.error('Logout error:', error); @@ -86,7 +90,7 @@ export const Header = ({ breadcrumbs, currentPage, onMenuClick }: HeaderProps): // Dispatch logout action to clear local state dispatch({ type: 'auth/logout' }); showToast.success(message, description); - navigate('/', { replace: true }); + navigate(redirectPath, { replace: true }); } }; diff --git a/src/components/layout/Layout.tsx b/src/components/layout/Layout.tsx index 5b266ec..e5620ba 100644 --- a/src/components/layout/Layout.tsx +++ b/src/components/layout/Layout.tsx @@ -29,10 +29,10 @@ export const Layout = ({ children, currentPage, breadcrumbs, pageHeader }: Layou return (