'use client'; import { useEffect, useState } from 'react'; import { authApi, wishlistsApi } from '@/lib/api'; type Status = 'checking' | { kind: 'ok'; guestName: string } | 'denied'; export default function GuestGuard({ children }: { children: React.ReactNode }) { const [status, setStatus] = useState('checking'); useEffect(() => { let cancelled = false; (async () => { const usr = new URL(window.location.href).searchParams.get('usr'); try { if (usr) { await authApi.session({ usr }); } const who = await authApi.whoami(); if (cancelled) return; if (who.role === 'admin' || who.role === 'guest') { setStatus({ kind: 'ok', guestName: who.guest?.name ?? 'admin' }); } else { const slug = window.location.pathname.split('/').filter(Boolean)[0]; if (slug) { const wishlist = await wishlistsApi.getBySlug(slug); if (cancelled) return; if (wishlist.isPublic) { setStatus({ kind: 'ok', guestName: 'visitante' }); return; } } setStatus('denied'); } } catch { if (!cancelled) setStatus('denied'); } })(); return () => { cancelled = true; }; }, []); if (status === 'checking') return
Verificando convite…
; if (status === 'denied') return

Convite necessário

Esta lista é por convite. Use o link que recebeu.

; return <>{children}; }