'use client'; import type { ReactNode } from 'react'; import { useEffect } from 'react'; import { usePathname, useRouter } from 'next/navigation'; import { LoadingState } from '@/components/feedback/loading-state'; import { useAuth } from '@/components/providers/auth-provider'; interface DashboardAuthGuardProps { children: ReactNode; } export function DashboardAuthGuard({ children }: DashboardAuthGuardProps) { const router = useRouter(); const pathname = usePathname(); const { isAuthenticated, isLoading } = useAuth(); useEffect(() => { if (!isLoading && !isAuthenticated) { const redirect = pathname && pathname !== '/' ? `?redirect=${encodeURIComponent(pathname)}` : ''; router.replace(`/login${redirect}`); } }, [isAuthenticated, isLoading, pathname, router]); if (isLoading || !isAuthenticated) { return (
); } return <>{children}; }