'use client'; import { useEffect } from 'react'; import { useRouter } from 'next/navigation'; import { authApi, wishlistsApi } from '@/lib/api'; export default function HomePage() { const router = useRouter(); useEffect(() => { let cancelled = false; (async () => { const params = new URL(window.location.href).searchParams; const adm = params.get('adm'); const usr = params.get('usr'); try { if (adm) { await authApi.session({ adm }); router.replace('/admin'); return; } if (usr) { await authApi.session({ usr }); } const who = await authApi.whoami(); if (cancelled) return; if (who.role === 'admin') { router.replace('/admin'); return; } // For guests (and after usr token auth), redirect to the single wishlist if (who.role === 'guest') { const lists = await wishlistsApi.getAllPublic(); if (cancelled) return; if (lists.length > 0) { router.replace(`/${lists[0].slug}`); } return; } // Unauthenticated — redirect to wishlist anyway (GuestGuard will block them there) const lists = await wishlistsApi.getAllPublic(); if (!cancelled && lists.length > 0) { router.replace(`/${lists[0].slug}`); } } catch { // ignore } })(); return () => { cancelled = true; }; }, [router]); return (
Carregando…