From b7ce3a822b4438c38496c05c4c589eb6f38c95e7 Mon Sep 17 00:00:00 2001
From: Alberto Defendi <1369-ahl-berto@users.noreply.gitlab.inf.unibz.it>
Date: Thu, 27 May 2021 10:19:26 +0200
Subject: [PATCH] Attach context.

---
 .../AuthUser/ChoseRole/ChoseRole.tsx          |  2 +-
 .../AuthUser/SignInForm/SignInForm.tsx        |  2 --
 .../Authorization/Authorization.tsx           | 22 +++++++++++++++----
 3 files changed, 19 insertions(+), 7 deletions(-)

diff --git a/src/components/AuthUser/ChoseRole/ChoseRole.tsx b/src/components/AuthUser/ChoseRole/ChoseRole.tsx
index c6fe0f5..ce2688c 100644
--- a/src/components/AuthUser/ChoseRole/ChoseRole.tsx
+++ b/src/components/AuthUser/ChoseRole/ChoseRole.tsx
@@ -12,7 +12,7 @@ import { AuthRoutes } from 'api/routes';
  */
 export const ChoseRole: FC = () => {
   const history = useHistory();
-  const [userRoles, setUserRoles] = useState<string[]>(['hello', 'world']);
+  const [userRoles, setUserRoles] = useState<string[]>(['']);
 
   const choseAndForward = (role: string): void => {
     // Set role in the server.
diff --git a/src/components/AuthUser/SignInForm/SignInForm.tsx b/src/components/AuthUser/SignInForm/SignInForm.tsx
index 528252e..8d9d8be 100644
--- a/src/components/AuthUser/SignInForm/SignInForm.tsx
+++ b/src/components/AuthUser/SignInForm/SignInForm.tsx
@@ -64,8 +64,6 @@ export const SignInForm: FC = () => {
         } else if (response.data.status === 'role-choice-needed') {
           history.replace(`${NonAuthRoutes.auth}${AuthRoutes.choseRole}`);
         } else if (response.data.status === 'success') {
-          setRole(response.data.role);
-          setIsAuth(true);
           history.replace(`${AuthRoutes.dashboard}${AuthRoutes.home}`);
         }
       });
diff --git a/src/components/Authorization/Authorization.tsx b/src/components/Authorization/Authorization.tsx
index 66ef96a..3a03f1f 100644
--- a/src/components/Authorization/Authorization.tsx
+++ b/src/components/Authorization/Authorization.tsx
@@ -1,7 +1,20 @@
+import React, { ComponentType, FC, useContext } from 'react';
+import { BlurCircular } from '@material-ui/icons';
+import { NonAuthRoutes } from 'api/routes';
+import { AuthContext } from 'components/AuthUser/AuthContext';
 import { Unauthorized } from 'components/NonAuthUser/Unauthorized/Unauthorized';
 import { useAuth } from 'hooks/useAuth';
 import { useRole } from 'hooks/useRole';
-import React, { ComponentType, FC } from 'react';
+import { Redirect } from 'react-router-dom';
+
+const HandleIsAuth: FC<{ isAuth: boolean }> = ({ isAuth }) =>
+  isAuth ? (
+    <Unauthorized />
+  ) : (
+    <Redirect
+      to={{ pathname: `${NonAuthRoutes.auth}${NonAuthRoutes.signIn}` }}
+    />
+  );
 
 interface WithAuthProps {
   allowedRoles: string[];
@@ -27,14 +40,15 @@ export const withAuthorization = <T extends WithAuthProps = WithAuthProps>(
   ) => {
     const { allowedRoles } = props as T;
 
-    const [role] = useRole();
-    const [isAuth] = useAuth();
+    const { role, isAuth } = useContext(AuthContext);
+
+    console.log(`ROLE ${role} AUTH ${isAuth}`);
 
     // props comes afterwards so the can override the default ones.
     return allowedRoles.includes(role) && isAuth ? (
       <WrappedComponent {...(props as T)} />
     ) : (
-      <Unauthorized />
+      <HandleIsAuth isAuth={isAuth} />
     );
   };
 
-- 
GitLab