AuthorizedRoute avec vérification roles

This commit is contained in:
wpetit 2020-03-26 13:56:00 +01:00
parent 29e112ae7d
commit 3a0da48455
2 changed files with 21 additions and 3 deletions

View File

@ -23,7 +23,9 @@ 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 path='/dashboard-dev' exact component={DashBoardDev} /> <AuthorizedRoute
roles={['ROLE_USER']} required='ROLE_DEV'
path='/dashboard-dev' exact component={DashBoardDev} />
<Route component={() => <Redirect to="/home" />} /> <Route component={() => <Redirect to="/home" />} />
</Switch> </Switch>
</HashRouter> </HashRouter>

View File

@ -1,7 +1,23 @@
import { Route } from 'react-router'; import { Route } from 'react-router';
import PropTypes from 'prop-types';
export function AuthorizedRoute({ required, roles, ...props}) {
if ( !roles.includes(required) ) {
return (
<div className="message is-danger">
<div className="message-body">
Vous ne pouvez pas accéder à cette page.
</div>
</div>
);
}
export function AuthorizedRoute({...props}) {
return ( return (
<Route {...props} /> <Route {...props} />
) );
} }
AuthorizedRoute.propTypes = {
required: PropTypes.string.isRequired,
roles: PropTypes.arrayOf(PropTypes.string).isRequired
};