22 lines
600 B
TypeScript
22 lines
600 B
TypeScript
import { Navigate, Outlet, useLocation } from 'react-router-dom';
|
|
import { useAuth } from './AuthContext';
|
|
|
|
export function PrivateRoute() {
|
|
const { isAuthenticated, isLoading } = useAuth();
|
|
const location = useLocation();
|
|
|
|
if (isLoading) {
|
|
return (
|
|
<div className="flex min-h-screen items-center justify-center bg-surface">
|
|
<div className="h-10 w-10 animate-spin rounded-full border-2 border-accent border-t-transparent" />
|
|
</div>
|
|
);
|
|
}
|
|
|
|
if (!isAuthenticated) {
|
|
return <Navigate to="/login" state={{ from: location }} replace />;
|
|
}
|
|
|
|
return <Outlet />;
|
|
}
|