diff --git a/app/api/public-songs/route.ts b/app/api/public-songs/route.ts new file mode 100644 index 0000000..e1410b0 --- /dev/null +++ b/app/api/public-songs/route.ts @@ -0,0 +1,21 @@ +import { NextResponse } from 'next/server'; +import { PrismaClient } from '@prisma/client'; + +const prisma = new PrismaClient(); + +// Öffentliche, schreibgeschützte Song-Liste für das Spiel (GuessInput etc.). +// Kein Auth, nur Lesen der nötigsten Felder. +export async function GET() { + const songs = await prisma.song.findMany({ + orderBy: { createdAt: 'desc' }, + select: { + id: true, + title: true, + artist: true, + }, + }); + + return NextResponse.json(songs); +} + + diff --git a/components/GuessInput.tsx b/components/GuessInput.tsx index 1e0c4b0..608cae8 100644 --- a/components/GuessInput.tsx +++ b/components/GuessInput.tsx @@ -22,9 +22,25 @@ export default function GuessInput({ onGuess, disabled }: GuessInputProps) { const [isOpen, setIsOpen] = useState(false); useEffect(() => { - fetch('/api/songs') - .then(res => res.json()) - .then(data => setSongs(data)); + fetch('/api/public-songs') + .then(res => { + if (!res.ok) { + throw new Error(`Failed to load songs: ${res.status}`); + } + return res.json(); + }) + .then(data => { + if (Array.isArray(data)) { + setSongs(data); + } else { + console.error('Unexpected songs payload in GuessInput:', data); + setSongs([]); + } + }) + .catch(err => { + console.error('Error loading songs for GuessInput:', err); + setSongs([]); + }); }, []); useEffect(() => {