import { useEffect } from 'react'; import { useLocation, useNavigate } from 'react-router-dom'; import { buildLoginRedirectUrl } from '~/utils'; import { useAuthContext } from '~/hooks'; export default function useAuthRedirect() { const { user, roles, isAuthenticated } = useAuthContext(); const navigate = useNavigate(); const location = useLocation(); useEffect(() => { const timeout = setTimeout(() => { if (isAuthenticated) { return; } if (location.pathname.startsWith('/login')) { navigate('/login', { replace: true }); return; } navigate(buildLoginRedirectUrl(location.pathname, location.search, location.hash), { replace: true, }); }, 300); return () => { clearTimeout(timeout); }; }, [isAuthenticated, navigate, location]); return { user, roles, isAuthenticated, }; }