From fadf957bd2dac0f925d995b2bd2610521c13d30b Mon Sep 17 00:00:00 2001
From: Alberto Defendi <1369-ahl-berto@users.noreply.gitlab.inf.unibz.it>
Date: Sat, 5 Jun 2021 09:08:22 +0200
Subject: [PATCH] Replace context with api.

This keeps the user logged in after tab closing, since state is stored
in the server.
---
 src/components/Authorization/Authorization.tsx | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/src/components/Authorization/Authorization.tsx b/src/components/Authorization/Authorization.tsx
index 08db3ce..f63265e 100644
--- a/src/components/Authorization/Authorization.tsx
+++ b/src/components/Authorization/Authorization.tsx
@@ -4,6 +4,8 @@ import { NonAuthRoutes } from 'api/routes';
 import { Redirect } from 'react-router-dom';
 import { Unauthorized } from 'components/Unauthorized/Unauthorized';
 import { AuthContext } from 'components/Auth/AuthContext';
+import { useRole } from 'hooks/useRole';
+import { useAuth } from 'hooks/useAuth';
 
 const HandleIsAuth: FC<{ isAuth: boolean }> = ({ isAuth }) =>
   isAuth ? (
@@ -35,11 +37,12 @@ export const withAuthorization = (allowedRoles: string[]) => <
   const ComponentWithAuthorization: FC<T> = (
     props: Omit<T, keyof WithAuthProps>,
   ) => {
-    const { role, isAuth } = useContext(AuthContext);
-    console.log(`ROLE ${role} AUTH ${isAuth}`);
+    const [role, setRole, isLoading] = useRole();
+    const [isAuth] = useAuth();
+    console.log(`ROLE ${role} AUTH ${isAuth} LOADED ${isLoading}`);
 
     /* eslint-disable no-nested-ternary */
-    return typeof isAuth === null && role === null ? (
+    return isAuth === null || isLoading ? (
       <BlurCircular />
     ) : // props comes afterwards so the can override the default ones.
     allowedRoles.includes(role) && isAuth ? (
-- 
GitLab