Skip to content

Commit

Permalink
feat: users without permissions will be logged out
Browse files Browse the repository at this point in the history
  • Loading branch information
sirtawast committed Jul 31, 2023
1 parent e96b7e9 commit 4a84f92
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
4 changes: 2 additions & 2 deletions frontend/benefit/handler/src/auth/AuthProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import AuthContext from 'shared/auth/AuthContext';
const AuthProvider = <P,>({
children,
}: React.PropsWithChildren<P>): JSX.Element => {
const userQuery = useUserQuery((user) => Boolean(user));
const userQuery = useUserQuery((user) => user);
return (
<AuthContext.Provider
value={{
isAuthenticated: userQuery.isSuccess && userQuery.data,
isAuthenticated: userQuery.isSuccess && Boolean(userQuery.data),
isLoading: userQuery.isLoading,
isError: userQuery.isError,
}}
Expand Down
28 changes: 21 additions & 7 deletions frontend/benefit/handler/src/hooks/useUserQuery.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,48 @@
import { AxiosError } from 'axios';
import { BackendEndpoint } from 'benefit-shared/backend-api/backend-api';
import { useRouter } from 'next/router';
import { useQuery, UseQueryResult } from 'react-query';
import useBackendAPI from 'shared/hooks/useBackendAPI';
import useLocale from 'shared/hooks/useLocale';
import User from 'shared/types/user';

import { ROUTES } from '../constants';
import useLogout from './useLogout';

// check that authentication is still alive in every 5 minutes
const FIVE_MINUTES = 5 * 60 * 1000;

const useUserQuery = <T = User>(
const useUserQuery = <T extends User>(
select?: (user: User) => T
): UseQueryResult<T, Error> => {
): UseQueryResult<T | User, AxiosError> => {
const router = useRouter();
const locale = useLocale();
const noPermissionLogout = useLogout();

// Don't fetch user state if status is logged out
const logout =
(router.route === '/login' || router.route === `${locale}/login`) &&
(router.route === ROUTES.LOGIN ||
router.route === `${locale}${ROUTES.LOGIN}`) &&
(router.asPath.includes('logout=true') ||
router.asPath.includes('userStateError=true'));
const { axios, handleResponse } = useBackendAPI();

const handleError = (error: Error): void => {
const handleError = (error: AxiosError): void => {
if (logout) {
void router.push(`${locale}/login?logout=true`);
void router.push(`${locale}${ROUTES.LOGIN}?logout=true`);
} else if (/40[13]/.test(error.message)) {
void router.push(`${locale}/login`);
void router.push(`${locale}${ROUTES.LOGIN}`);
} else if (
!process.env.NEXT_PUBLIC_MOCK_FLAG ||
process.env.NEXT_PUBLIC_MOCK_FLAG === '0'
) {
void router.push(`${locale}/login?userStateError=true`);
void router.push(`${locale}${ROUTES.LOGIN}?userStateError=true`);
}
};

const checkForStaffStatus = (user: User): void => {
if (user && !user.is_staff) {
void noPermissionLogout();
}
};

Expand All @@ -41,6 +54,7 @@ const useUserQuery = <T = User>(
enabled: !logout,
retry: false,
select,
onSuccess: checkForStaffStatus,
onError: (error) => handleError(error),
}
);
Expand Down

0 comments on commit 4a84f92

Please sign in to comment.