'use client'; import { useEffect, useState } from 'react'; import { usePathname, useRouter } from 'next/navigation'; import { useAuthStore } from '@/stores/useAuthStore'; export function AuthGuard({ children }: { children: React.ReactNode }) { const pathname = usePathname(); const router = useRouter(); const { isAuthenticated, checkAuth } = useAuthStore(); const [checked, setChecked] = useState(false); useEffect(() => { checkAuth(); setChecked(true); }, [checkAuth]); useEffect(() => { if (!checked) return; if (!isAuthenticated && pathname !== '/login') { router.replace('/login'); } if (isAuthenticated && pathname === '/login') { router.replace('/products'); } }, [checked, isAuthenticated, pathname, router]); if (!checked) return null; if (!isAuthenticated && pathname !== '/login') return null; return <>{children}; }