import React , { memo } from 'react' ; import { Redirect , RouteComponentProps } from 'react-router-dom' ; import store from '../store/index' ; import { businessRouteList } from '../router/utils' ; import { getToken } from '../utils/cookie' ; import { IRoute } from '../router/config' ; import config from '../config/index' ; interface AuthProps extends RouteComponentProps { route : IRoute ; children : React . ReactNode ; } function checkAuth ( location ) { const { flattenRoutes } = store . getState ( ) . app ; const route = businessRouteList . find ( child => child . path === location . pathname ) ; if ( ! route ) { return true ; } if ( route . redirect ) { return true ; } if ( route . auth === false ) { return true ; } if ( ! flattenRoutes . find ( child => child . path === location . pathname ) ) { return false ; } return true ; } function Auth ( props ) { if ( ! getToken ( ) ) { return ( < Redirect to = { `/system/login?redirectURL=${ encodeURIComponent ( window . location . origin + config . BASENAME + props . location . pathname + props . location . search ) }` } />); } if ( ! checkAuth ( props . location ) ) { return < Redirect to = "/error/403" push / > ; } if ( props . route . redirect ) { return < Redirect to = { props . route . redirect ! } push / > ; } return < > { props . children } < />; } export default memo ( Auth ) ;