All files / pages/Auth ProtectedPage.tsx

100% Statements 7/7
100% Branches 6/6
100% Functions 1/1
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29                                  4x 1x   3x 1x 2x 1x   1x      
import type { ReactNode } from "react";
import { hasRole, useCurrentUser } from "main/utils/currentUser";
import AccessDeniedPage from "main/pages/Auth/AccessDeniedPage";
import PromptSignInPage from "main/pages/Auth/PromptSignInPage";
import LoadingPage from "main/pages/Auth/LoadingPage";
 
type ProtectedPageProps = {
  component?: ReactNode;
  currentUser: ReturnType<typeof useCurrentUser> & { initialData?: boolean };
  enforceRole: string;
};
 
export default function ProtectedPage({
  component,
  currentUser,
  enforceRole,
}: ProtectedPageProps): React.JSX.Element {
  if (currentUser.initialData) {
    return <LoadingPage />;
  }
  if (hasRole(currentUser, enforceRole)) {
    return <>{component}</>;
  } else if (!currentUser.loggedIn) {
    return <PromptSignInPage />;
  } else {
    return <AccessDeniedPage />;
  }
}