'use client' import { useLayoutEffect, useState } from 'react' import { useRouter } from 'next/navigation' import { isAuthenticated } from '@/lib/auth' interface AuthGuardProps { children: React.ReactNode } export function AuthGuard({ children }: AuthGuardProps) { const router = useRouter() // Initialize state synchronously to avoid cascading renders const [authState] = useState(() => { if (typeof window === 'undefined') return { checking: true, authed: false } const authed = isAuthenticated() return { checking: false, authed } }) useLayoutEffect(() => { if (!authState.checking && !authState.authed) { router.replace('/login') } }, [authState, router]) if (authState.checking) { return (
) } if (!authState.authed) { return null } return <>{children} }