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

Replace localstorage with data from AuthContext.

parent 996a93f8
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.
This commit is part of merge request !43. Comments created here will be created in the context of that merge request.
import React, { useState, useEffect } from 'react'; import React, { useState, useEffect, useContext } from 'react';
import axios from 'axios'; 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';
/** /**
* 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.
...@@ -21,6 +22,7 @@ export const PrivateRoute = ({ ...@@ -21,6 +22,7 @@ export const PrivateRoute = ({
}: 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 [loading, setLoading] = useState<boolean>(false);
const { role } = useContext(AuthContext);
useEffect(() => { useEffect(() => {
const fetch = async (): Promise<unknown> => { const fetch = async (): Promise<unknown> => {
...@@ -32,8 +34,7 @@ export const PrivateRoute = ({ ...@@ -32,8 +34,7 @@ export const PrivateRoute = ({
fetch(); fetch();
}, []); }, []);
const currentRole = String(sessionStorage.getItem('ROLE')); const userHasRequiredRole = requiredRoles.includes(role);
const userHasRequiredRole = requiredRoles.includes(currentRole);
const message = userHasRequiredRole const message = userHasRequiredRole
? 'Please log in to view this page' ? 'Please log in to view this page'
: 'Your role is not allowed'; : 'Your role is not allowed';
......
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