mirror of
https://github.com/AeThex-Corporation/AeThex-OS.git
synced 2026-04-17 22:07:20 +00:00
40 lines
974 B
TypeScript
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}</>;
|
|
}
|