import { NextResponse, type NextRequest } from 'next/server'; import { getCustomerUser } from '@/app/lib/lumen-auth'; import { isAdmin } from '@/app/lib/admin'; import { deleteImage } from '@/app/lib/cf-images'; export const runtime = 'nodejs'; export const dynamic = 'force-dynamic'; // Delete a single image from Cloudflare Images by id. Admin-only. // // Auth note: /api/* is NOT covered by middleware.ts (which gates /admin/* // and /orders/* only) — we MUST check the session here, otherwise the route // is reachable by anyone who finds it. export async function POST(req: NextRequest) { const user = await getCustomerUser(); if (!user || !isAdmin(user.email)) { return NextResponse.json({ error: 'forbidden' }, { status: 403 }); } let body: { id?: unknown }; try { body = (await req.json()) as { id?: unknown }; } catch { return NextResponse.json({ error: 'invalid json' }, { status: 400 }); } const id = typeof body.id === 'string' ? body.id.trim() : ''; if (!id) return NextResponse.json({ error: 'id required' }, { status: 400 }); try { await deleteImage(id); console.log(`[admin/images/delete] ${user.email} deleted ${id}`); return NextResponse.json({ ok: true, id }); } catch (e) { const msg = e instanceof Error ? e.message : 'delete failed'; console.error('[admin/images/delete]', msg); return NextResponse.json({ error: msg }, { status: 502 }); } }