aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/client/components/ProtectedRoute.tsx
blob: a0eb2ee911b6a6c95937c702c8573165ae86e6b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import { useAtomValue } from "jotai";
import type { ReactNode } from "react";
import { Redirect } from "wouter";
import { authLoadingAtom, isAuthenticatedAtom } from "../atoms";

export interface ProtectedRouteProps {
	children: ReactNode;
}

export function ProtectedRoute({ children }: ProtectedRouteProps) {
	const isAuthenticated = useAtomValue(isAuthenticatedAtom);
	const isLoading = useAtomValue(authLoadingAtom);

	if (isLoading) {
		return <output>Loading...</output>;
	}

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

	return <>{children}</>;
}