32 lines
743 B
TypeScript
32 lines
743 B
TypeScript
"use client";
|
|
|
|
import { useAuth } from "@/hooks/useAuth";
|
|
import { useEffect } from "react";
|
|
import { useRouter } from "next/navigation";
|
|
|
|
interface ProtectedRouteProps {
|
|
children: React.ReactNode;
|
|
fallback?: React.ReactNode;
|
|
}
|
|
|
|
export function ProtectedRoute({ children, fallback }: ProtectedRouteProps) {
|
|
const { isAuthenticated, isLoading } = useAuth();
|
|
const router = useRouter();
|
|
|
|
useEffect(() => {
|
|
if (!isLoading && !isAuthenticated) {
|
|
router.push("/login");
|
|
}
|
|
}, [isLoading, isAuthenticated, router]);
|
|
|
|
if (isLoading) {
|
|
return fallback || <div className="flex items-center justify-center min-h-screen">Carregando...</div>;
|
|
}
|
|
|
|
if (!isAuthenticated) {
|
|
return null;
|
|
}
|
|
|
|
return <>{children}</>;
|
|
}
|