Files
chadebebe/app/page.tsx
Adriano Belisario 23114637ac
Some checks failed
Build and Push Docker Image / build-and-push (push) Has been cancelled
fix: sync reorder transaction + redirect unauthenticated users to wishlist
- reorder route used async callback with better-sqlite3 (sync driver),
  causing "Transaction function cannot return a promise" — converted to sync
- home page now redirects unauthenticated visitors to the wishlist slug
  instead of getting stuck on the loading screen

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-03 21:02:10 +00:00

60 lines
1.7 KiB
TypeScript

'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 (
<div className="min-h-screen flex items-center justify-center text-center px-6">
<div className="max-w-xl">
<p className="text-lg text-gray-500">Carregando</p>
</div>
</div>
);
}