Skip to content
Snippets Groups Projects

Feature/authorization/curry

Merged Defendi Alberto requested to merge feature/authorization/curry into dev
1 file
+ 4
3
Compare changes
  • Side-by-side
  • Inline
@@ -25,15 +25,16 @@ interface WithAuthProps {
@@ -25,15 +25,16 @@ interface WithAuthProps {
* This creates a "personal area" in the working implementation.
* This creates a "personal area" in the working implementation.
* @returns {FC<T>} wrapped component.
* @returns {FC<T>} wrapped component.
*/
*/
export const withAuthorization = <T extends WithAuthProps = WithAuthProps>(
 
export const withAuthorization = (allowedRoles: string[]) => <
 
T extends WithAuthProps = WithAuthProps
 
>(
WrappedComponent: React.ComponentType<T>,
WrappedComponent: React.ComponentType<T>,
): FC<T> => {
): FC<T> => {
// Creating the inner component. The calculated Props type here is the where the magic happens.
// Creating the inner component. The calculated Props type here is the where the magic happens.
const ComponentWithAuthorization: FC<T> = (
const ComponentWithAuthorization: FC<T> = (
props: Omit<T, keyof WithAuthProps>,
props: Omit<T, keyof WithAuthProps>,
) => {
) => {
const { allowedRoles } = props as T;
const { role, isAuth } = useContext(AuthContext);
const { role, isAuth } = useContext(AuthContext);
console.log(`ROLE ${role} AUTH ${isAuth}`);
console.log(`ROLE ${role} AUTH ${isAuth}`);
Loading