diff --git a/app/components/PageRedirectHandler.tsx b/app/components/PageRedirectHandler.tsx index e57120afb3..a9092e7635 100644 --- a/app/components/PageRedirectHandler.tsx +++ b/app/components/PageRedirectHandler.tsx @@ -12,77 +12,78 @@ interface Props { pageComponent: CiipPageComponent; } +const sessionQuery = graphql` + query PageRedirectHandlerQuery { + session { + userGroups + ciipUserBySub { + __typename + } + } + } +`; + const PageRedirectHandler: React.FunctionComponent = ({ children, environment, pageComponent }) => { - const sessionQuery = graphql` - query PageRedirectHandlerQuery { - session { - userGroups - ciipUserBySub { - __typename - } - } - } - `; - const [shouldRender, setShouldRender] = useState(false); const router = useRouter(); - useEffect(() => { - if (!shouldRender) { - checkSessionAndGroups(); - } - }); + const checkSessionAndGroups = async () => { + const {isAccessProtected, allowedGroups = []} = pageComponent; + const response = await fetchQuery( + environment, + sessionQuery, + {} + ); + if (isAccessProtected && !response?.session) { + router.push({ + pathname: '/login-redirect', + query: { + redirectTo: router.asPath + } + }); + return; + } - const checkSessionAndGroups = async () => { - const {isAccessProtected, allowedGroups = []} = pageComponent; - const response = await fetchQuery( - environment, - sessionQuery, - {} - ); - if (isAccessProtected && !response?.session) { - router.push({ - pathname: '/login-redirect', - query: { - redirectTo: router.asPath - } - }); - return null; - } + const userGroups = response?.session?.userGroups || ['Guest']; - const userGroups = response?.session?.userGroups || ['Guest']; + const canAccess = + allowedGroups.length === 0 || + userGroups.some((g) => allowedGroups.includes(g)); - const canAccess = - allowedGroups.length === 0 || - userGroups.some((g) => allowedGroups.includes(g)); + // Redirect users attempting to access a page that their group doesn't allow + // to their landing route. This needs to happen before redirecting to the registration + // to ensure that a pending analyst doesn't get redirect to the registration page + if (!canAccess) { + router.push({ + pathname: getUserGroupLandingRoute(userGroups) + }); + return; + } - // Redirect users attempting to access a page that their group doesn't allow - // to their landing route. This needs to happen before redirecting to the registration - // to ensure that a pending analyst doesn't get redirect to the registration page - if (!canAccess) { - router.push({ - pathname: getUserGroupLandingRoute(userGroups) - }); - return null; - } + if ( + isAccessProtected && + !response?.session?.ciipUserBySub && + router.route !== '/registration' + ) { + router.push({ + pathname: '/registration', + query: { + redirectTo: router.asPath + } + }); + return; + } - if (isAccessProtected && !response?.session?.ciipUserBySub) { - router.push({ - pathname: '/registration', - query: { - redirectTo: router.asPath - } - }); - return null; - } + setShouldRender(true); + }; - setShouldRender(true); - }; + checkSessionAndGroups(); + }, [pageComponent, router, environment]); // eslint-disable-next-line react/jsx-no-useless-fragment if (shouldRender) return <>{children};