AeThex-OS/shell/aethex-shell/src-webos/components/ProtectedRoute.tsx

40 lines
974 B
TypeScript

import { useEffect, useRef } from "react";
import { useLocation } from "wouter";
import { useAuth } from "@/lib/auth";
interface ProtectedRouteProps {
children: React.ReactNode;
}
export function ProtectedRoute({ children }: ProtectedRouteProps) {
const { isAuthenticated, isLoading, user } = useAuth();
const [, setLocation] = useLocation();
const wasAuthenticated = useRef(false);
if (isAuthenticated) {
wasAuthenticated.current = true;
}
useEffect(() => {
if (!isLoading && !isAuthenticated) {
setLocation("/login");
}
}, [isLoading, isAuthenticated, setLocation]);
if (isLoading) {
if (wasAuthenticated.current || user) {
return <>{children}</>;
}
return (
<div className="min-h-screen bg-background flex items-center justify-center">
<div className="text-primary animate-pulse">Loading...</div>
</div>
);
}
if (!isAuthenticated) {
return null;
}
return <>{children}</>;
}