Skip to content
Snippets Groups Projects

Move back cookie fetch to SignInForm. Role fetched and saved into a Context. Small refactorings.

Merged Defendi Alberto requested to merge feature/role into dev
1 file
+ 2
4
Compare changes
  • Side-by-side
  • Inline
@@ -3,6 +3,7 @@ import axios from 'axios';
import { Route, Redirect, RouteProps } from 'react-router-dom';
import { NonAuthRoutes } from 'api/routes';
import { AuthContext } from 'components/AuthUser/AuthContext';
import { Roles } from 'api/userRoles';
/**
* A wrapper for <Route> that redirects to the login screen if you're not yet authenticated.
@@ -22,14 +23,12 @@ export const PrivateRoute = ({
requiredRoles,
}: Props): JSX.Element => {
const [auth, setAuth] = useState<boolean>(false);
const [loading, setLoading] = useState<boolean>(false);
const { role } = useContext(AuthContext);
useEffect(() => {
const fetch = async (): Promise<unknown> => {
const result = await axios('/api/web/login/is_authenticated');
setAuth(result.data.is_authenticated);
setLoading(true);
return null;
};
/*
@@ -38,9 +37,8 @@ export const PrivateRoute = ({
this component state is triggered, falling in unnecessary calls to the
server.
*/
if (role) fetch();
if (role !== Roles.visitor) fetch();
}, [auth]);
const userHasRequiredRole = requiredRoles.includes(role);
const message = userHasRequiredRole
? 'Please log in to view this page'
Loading