Skip to content
Snippets Groups Projects

Fix/authorization

Merged Defendi Alberto requested to merge fix/authorization into dev
1 file
+ 4
2
Compare changes
  • Side-by-side
  • Inline
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 React, { ComponentType, FC } from 'react';
import { useAuth } from 'hooks/useAuth';
import { useRole } from 'hooks/useRole';
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[];
@@ -25,14 +40,15 @@ export const withAuthorization = <T extends WithAuthProps = WithAuthProps>(
) => {
const { allowedRoles } = props as T;
const role = 'admin';
const isAuth = true;
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} />
);
};
Loading