Ajout AuthorizedComponent

This commit is contained in:
wpetit 2020-03-26 14:16:55 +01:00
parent 91049a76b9
commit 9917db8891
1 changed files with 26 additions and 0 deletions

View File

@ -0,0 +1,26 @@
import React from 'react';
import { Route } from 'react-router';
import PropTypes from 'prop-types';
import { useSelector } from 'react-redux';
export function AuthorizedComponent({ required, roles, children }) {
if ( !roles.includes(required) ) {
return null;
}
return (
<>
{children}
</>
);
}
export function ConnectedAuthorizedComponent({ ...props }) {
const roles = useSelector(({ session }) => ( session.user ? session.user.roles : [] ));
return <AuthorizedComponent roles={roles} {...props} />
}
AuthorizedComponent.propTypes = {
required: PropTypes.string.isRequired,
roles: PropTypes.arrayOf(PropTypes.string).isRequired
};