40 lines
986 B
TypeScript
Executable File
40 lines
986 B
TypeScript
Executable File
import AppLayout from "@/components/layout/app-layout";
|
|
import LoadingScreen from "@/components/ui/LoadingScreen";
|
|
import { useAuth } from "@/hooks/use-auth";
|
|
import { Suspense } from "react";
|
|
import { Redirect, Route } from "wouter";
|
|
|
|
type ComponentLike = React.ComponentType; // works for both lazy() and regular components
|
|
|
|
export function ProtectedRoute({
|
|
path,
|
|
component: Component,
|
|
adminOnly = false,
|
|
}: {
|
|
path: string;
|
|
component: ComponentLike;
|
|
adminOnly?: boolean;
|
|
}) {
|
|
const { user, isLoading } = useAuth();
|
|
|
|
return (
|
|
<Route path={path}>
|
|
{isLoading ? (
|
|
<AppLayout>
|
|
<LoadingScreen />
|
|
</AppLayout>
|
|
) : !user ? (
|
|
<Redirect to="/auth" />
|
|
) : adminOnly && user.username !== "admin" ? (
|
|
<Redirect to="/insurance-status" />
|
|
) : (
|
|
<AppLayout>
|
|
<Suspense fallback={<LoadingScreen />}>
|
|
<Component />
|
|
</Suspense>
|
|
</AppLayout>
|
|
)}
|
|
</Route>
|
|
);
|
|
}
|