Skip to content

Commit

Permalink
fix: PageRedirectHandler should always checkSessionAndGroups
Browse files Browse the repository at this point in the history
  • Loading branch information
matthieu-foucault committed Jul 16, 2020
1 parent 8b80970 commit ca417dc
Showing 1 changed file with 57 additions and 56 deletions.
113 changes: 57 additions & 56 deletions app/components/PageRedirectHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,77 +12,78 @@ interface Props {
pageComponent: CiipPageComponent;
}

const sessionQuery = graphql`
query PageRedirectHandlerQuery {
session {
userGroups
ciipUserBySub {
__typename
}
}
}
`;

const PageRedirectHandler: React.FunctionComponent<Props> = ({
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<PageRedirectHandlerQuery>(
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<PageRedirectHandlerQuery>(
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}</>;
Expand Down

0 comments on commit ca417dc

Please sign in to comment.