Skip to content
Snippets Groups Projects
Verified Commit de68ec89 authored by Defendi Alberto's avatar Defendi Alberto
Browse files

Remove setLoading and insert visitor for stronger check.

parent 41fb2d2b
No related branches found
No related tags found
2 merge requests!56Refined auth flow and new website pages.,!43Move back cookie fetch to SignInForm. Role fetched and saved into a Context. Small refactorings.
Pipeline #12173 passed
...@@ -3,6 +3,7 @@ import axios from 'axios'; ...@@ -3,6 +3,7 @@ import axios from 'axios';
import { Route, Redirect, RouteProps } from 'react-router-dom'; import { Route, Redirect, RouteProps } from 'react-router-dom';
import { NonAuthRoutes } from 'api/routes'; import { NonAuthRoutes } from 'api/routes';
import { AuthContext } from 'components/AuthUser/AuthContext'; 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. * A wrapper for <Route> that redirects to the login screen if you're not yet authenticated.
...@@ -22,14 +23,12 @@ export const PrivateRoute = ({ ...@@ -22,14 +23,12 @@ export const PrivateRoute = ({
requiredRoles, requiredRoles,
}: Props): JSX.Element => { }: Props): JSX.Element => {
const [auth, setAuth] = useState<boolean>(false); const [auth, setAuth] = useState<boolean>(false);
const [loading, setLoading] = useState<boolean>(false);
const { role } = useContext(AuthContext); const { role } = useContext(AuthContext);
useEffect(() => { useEffect(() => {
const fetch = async (): Promise<unknown> => { const fetch = async (): Promise<unknown> => {
const result = await axios('/api/web/login/is_authenticated'); const result = await axios('/api/web/login/is_authenticated');
setAuth(result.data.is_authenticated); setAuth(result.data.is_authenticated);
setLoading(true);
return null; return null;
}; };
/* /*
...@@ -38,9 +37,8 @@ export const PrivateRoute = ({ ...@@ -38,9 +37,8 @@ export const PrivateRoute = ({
this component state is triggered, falling in unnecessary calls to the this component state is triggered, falling in unnecessary calls to the
server. server.
*/ */
if (role) fetch(); if (role !== Roles.visitor) fetch();
}, [auth]); }, [auth]);
const userHasRequiredRole = requiredRoles.includes(role); const userHasRequiredRole = requiredRoles.includes(role);
const message = userHasRequiredRole const message = userHasRequiredRole
? 'Please log in to view this page' ? 'Please log in to view this page'
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment