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

Attach context.

parent 903426cb
No related branches found
No related tags found
2 merge requests!56Refined auth flow and new website pages.,!50Fix/authorization
Pipeline #12383 passed
...@@ -12,7 +12,7 @@ import { AuthRoutes } from 'api/routes'; ...@@ -12,7 +12,7 @@ import { AuthRoutes } from 'api/routes';
*/ */
export const ChoseRole: FC = () => { export const ChoseRole: FC = () => {
const history = useHistory(); const history = useHistory();
const [userRoles, setUserRoles] = useState<string[]>(['hello', 'world']); const [userRoles, setUserRoles] = useState<string[]>(['']);
const choseAndForward = (role: string): void => { const choseAndForward = (role: string): void => {
// Set role in the server. // Set role in the server.
......
...@@ -64,8 +64,6 @@ export const SignInForm: FC = () => { ...@@ -64,8 +64,6 @@ export const SignInForm: FC = () => {
} else if (response.data.status === 'role-choice-needed') { } else if (response.data.status === 'role-choice-needed') {
history.replace(`${NonAuthRoutes.auth}${AuthRoutes.choseRole}`); history.replace(`${NonAuthRoutes.auth}${AuthRoutes.choseRole}`);
} else if (response.data.status === 'success') { } else if (response.data.status === 'success') {
setRole(response.data.role);
setIsAuth(true);
history.replace(`${AuthRoutes.dashboard}${AuthRoutes.home}`); history.replace(`${AuthRoutes.dashboard}${AuthRoutes.home}`);
} }
}); });
......
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 { Unauthorized } from 'components/NonAuthUser/Unauthorized/Unauthorized';
import { useAuth } from 'hooks/useAuth'; import { useAuth } from 'hooks/useAuth';
import { useRole } from 'hooks/useRole'; 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 { interface WithAuthProps {
allowedRoles: string[]; allowedRoles: string[];
...@@ -27,14 +40,15 @@ export const withAuthorization = <T extends WithAuthProps = WithAuthProps>( ...@@ -27,14 +40,15 @@ export const withAuthorization = <T extends WithAuthProps = WithAuthProps>(
) => { ) => {
const { allowedRoles } = props as T; const { allowedRoles } = props as T;
const [role] = useRole(); const { role, isAuth } = useContext(AuthContext);
const [isAuth] = useAuth();
console.log(`ROLE ${role} AUTH ${isAuth}`);
// props comes afterwards so the can override the default ones. // props comes afterwards so the can override the default ones.
return allowedRoles.includes(role) && isAuth ? ( return allowedRoles.includes(role) && isAuth ? (
<WrappedComponent {...(props as T)} /> <WrappedComponent {...(props as T)} />
) : ( ) : (
<Unauthorized /> <HandleIsAuth isAuth={isAuth} />
); );
}; };
......
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