Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.0 kB
0
Indexable
Never
import React from 'react';
import { Route, Redirect, RouteProps } from 'react-router-dom';
import { getStateStore } from 'src/global-state/Store';
import { isUserHasPermissions } from 'src/utils/UserPermissions/hasPermissions';
interface ProtectedRouteProps extends RouteProps {
  render: (props: any) => JSX.Element;
  permissions: (match: any) => string[];
}

const ProtectedRoute: React.FC<ProtectedRouteProps> = ({ render, permissions, ...rest }) => {
  console.log({ permissions });
  return (
    <Route
      {...rest}
      render={(props) => {
        const userPermissions = getStateStore().user.permissions;
        console.log('ProtectedRoute:--->', { userPermissions });
        const hasPermission = isUserHasPermissions(permissions(props.match));
        return hasPermission ? (
          render(props)
        ) : (
          <Redirect
            to={{
              pathname: '/',
              state: { from: props.location },
            }}
          />
        );
      }}
    />
  );
};

export default ProtectedRoute;