index.js 555 B

1234567891011121314
  1. import { Navigate, Route, useLocation } from 'react-router-dom';
  2. import { connect } from 'react-redux';
  3. export const ProtectedRoute = ({ roles = ['anon'], children, fallback = '/', auth } = {}) => {
  4. let location = useLocation();
  5. !!auth.length || (auth = ['anon']);
  6. if (!auth.filter((role) => roles.includes(role)).length) {
  7. return <Navigate to={fallback} state={{ from: location }} />;
  8. }
  9. return children;
  10. };
  11. export const CProtectedRoute = connect((state) => ({ auth: state.auth?.payload?.sub?.acl || [] }))(ProtectedRoute);