diff --git a/src/components/AuthUser/ChoseRole/ChoseRole.tsx b/src/components/AuthUser/ChoseRole/ChoseRole.tsx index c6fe0f5025f5c98b0a9cb9bc30b6e084e3f53318..ce2688cef4be323d88f8f1377f84698f6924595f 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 528252e4896ebc948ba244a2c676aa25248414b4..8d9d8be38f17394c0182967a94ffea9639102b28 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 66ef96a9ab7790b9744dc4d47d2ebc094aa44c49..3a03f1f5ba2277ef38873820ac64d62a49f2baf7 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} /> ); };