AuthorizedRoute connecté

This commit is contained in:
wpetit 2020-03-26 14:03:07 +01:00
parent 3a0da48455
commit 91049a76b9
2 changed files with 8 additions and 3 deletions

View File

@ -8,7 +8,7 @@ import DashBoardDev from './pages/DashBoardDev';
import { ConnectedLogoutPage as LogoutPage } from './pages/logout'; import { ConnectedLogoutPage as LogoutPage } from './pages/logout';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { history } from './util/history'; import { history } from './util/history';
import { AuthorizedRoute } from './components/AuthorizedRoute'; import { ConnectedAuthorizedRoute as AuthorizedRoute } from './components/AuthorizedRoute';
const LazyHomePage = React.lazy(() => import('./pages/home.lazy')); const LazyHomePage = React.lazy(() => import('./pages/home.lazy'));
@ -23,8 +23,7 @@ export class App extends Component {
<Route path='/logout' exact component={LogoutPage} /> <Route path='/logout' exact component={LogoutPage} />
<Route path='/home' exact component={LazyHomePage} /> <Route path='/home' exact component={LazyHomePage} />
<Route path='/dashboard-client' exact component={DashBoardClient} /> <Route path='/dashboard-client' exact component={DashBoardClient} />
<AuthorizedRoute <AuthorizedRoute required='ROLE_DEVELOPER'
roles={['ROLE_USER']} required='ROLE_DEV'
path='/dashboard-dev' exact component={DashBoardDev} /> path='/dashboard-dev' exact component={DashBoardDev} />
<Route component={() => <Redirect to="/home" />} /> <Route component={() => <Redirect to="/home" />} />
</Switch> </Switch>

View File

@ -1,5 +1,6 @@
import { Route } from 'react-router'; import { Route } from 'react-router';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { useSelector } from 'react-redux';
export function AuthorizedRoute({ required, roles, ...props}) { export function AuthorizedRoute({ required, roles, ...props}) {
if ( !roles.includes(required) ) { if ( !roles.includes(required) ) {
@ -17,6 +18,11 @@ export function AuthorizedRoute({ required, roles, ...props}) {
); );
} }
export function ConnectedAuthorizedRoute({ ...props }) {
const roles = useSelector(({ session }) => ( session.user ? session.user.roles : [] ));
return <AuthorizedRoute roles={roles} {...props} />
}
AuthorizedRoute.propTypes = { AuthorizedRoute.propTypes = {
required: PropTypes.string.isRequired, required: PropTypes.string.isRequired,
roles: PropTypes.arrayOf(PropTypes.string).isRequired roles: PropTypes.arrayOf(PropTypes.string).isRequired