Authorized.tsx 761 Bytes
Newer Older
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
1
import React from 'react';
2 3 4 5
import check, { IAuthorityType } from './CheckPermissions';

import AuthorizedRoute from './AuthorizedRoute';
import Secured from './Secured';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
6

7
interface AuthorizedProps {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
8 9 10 11
  authority: IAuthorityType;
  noMatch?: React.ReactNode;
}

12
type IAuthorizedType = React.FunctionComponent<AuthorizedProps> & {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
13 14 15 16 17
  Secured: typeof Secured;
  check: typeof check;
  AuthorizedRoute: typeof AuthorizedRoute;
};

18
const Authorized: React.FunctionComponent<AuthorizedProps> = ({
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
19 20 21 22 23
  children,
  authority,
  noMatch = null,
}) => {
  const childrenRender: React.ReactNode = typeof children === 'undefined' ? null : children;
24
  const dom = check(authority, childrenRender, noMatch);
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
25 26 27 28
  return <>{dom}</>;
};

export default Authorized as IAuthorizedType;