fleet/frontend/components/AuthAnyAdminRoutes/AuthAnyAdminRoutes.tsx

31 lines
714 B
TypeScript
Raw Normal View History

import React, { useContext } from "react";
import { useErrorHandler } from "react-error-boundary";
import { AppContext } from "context/app";
2021-10-26 14:24:16 +00:00
interface IAuthAnyAdminRoutesProps {
children: JSX.Element;
}
/**
* Checks if a user is any maintainer or any admin when routing
*/
2021-10-26 14:24:16 +00:00
const AuthAnyAdminRoutes = ({
children,
2021-10-26 14:24:16 +00:00
}: IAuthAnyAdminRoutesProps): JSX.Element | null => {
const handlePageError = useErrorHandler();
const { currentUser, isGlobalAdmin, isAnyTeamAdmin } = useContext(AppContext);
if (!currentUser) {
return null;
}
if (!isGlobalAdmin && !isAnyTeamAdmin) {
handlePageError({ status: 403 });
return null;
}
return <>{children}</>;
};
2021-10-26 14:24:16 +00:00
export default AuthAnyAdminRoutes;