aboutsummaryrefslogtreecommitdiffhomepage
path: root/frontend/src/components/ProtectedRoute.tsx
blob: 206e5c5edea87d835a539fb27212864558944a60 (plain)
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
30
31
32
import type { ReactNode } from "react";
import { Redirect } from "wouter";
import { useAuth } from "../contexts/AuthContext";

interface Props {
	children: ReactNode;
}

export function ProtectedRoute({ children }: Props) {
	const { isLoggedIn, isLoading } = useAuth();

	if (isLoading) {
		return (
			<div
				style={{
					display: "flex",
					justifyContent: "center",
					alignItems: "center",
					minHeight: "100vh",
				}}
			>
				Loading...
			</div>
		);
	}

	if (!isLoggedIn) {
		return <Redirect to="/login" />;
	}

	return <>{children}</>;
}