'use client'; import { useEffect, useState } from 'react'; import { useSearchParams } from 'next/navigation'; import { authApi } from '@/lib/api'; export default function AdminGuard({ children }: { children: React.ReactNode }) { const params = useSearchParams(); const [state, setState] = useState<'checking' | 'ok' | 'denied'>('checking'); useEffect(() => { let cancelled = false; (async () => { const adm = params.get('adm'); try { if (adm) { await authApi.session({ adm }); // strip the param from URL but keep route const url = new URL(window.location.href); url.searchParams.delete('adm'); window.history.replaceState({}, '', url.toString()); } const who = await authApi.whoami(); if (cancelled) return; if (who.role === 'admin') setState('ok'); else setState('denied'); } catch { if (!cancelled) setState('denied'); } })(); return () => { cancelled = true; }; }, [params]); if (state === 'checking') return
Verificando…
; if (state === 'denied') return

Acesso restrito

Adicione ?adm=<token> à URL para entrar.

; return <>{children}; }