Lumen & Gesso / website · lumengesso.com / app / api / admin / images / delete / route.ts
| 1 | import { NextResponse, type NextRequest } from 'next/server'; |
| 2 | import { getCustomerUser } from '@/app/lib/lumen-auth'; |
| 3 | import { isAdmin } from '@/app/lib/admin'; |
| 4 | import { deleteImage } from '@/app/lib/cf-images'; |
| 5 | |
| 6 | export const runtime = 'nodejs'; |
| 7 | export const dynamic = 'force-dynamic'; |
| 8 | |
| 9 | // Delete a single image from Cloudflare Images by id. Admin-only. |
| 10 | // |
| 11 | // Auth note: /api/* is NOT covered by middleware.ts (which gates /admin/* |
| 12 | // and /orders/* only) — we MUST check the session here, otherwise the route |
| 13 | // is reachable by anyone who finds it. |
| 14 | export async function POST(req: NextRequest) { |
| 15 | const user = await getCustomerUser(); |
| 16 | if (!user || !isAdmin(user.email)) { |
| 17 | return NextResponse.json({ error: 'forbidden' }, { status: 403 }); |
| 18 | } |
| 19 | |
| 20 | let body: { id?: unknown }; |
| 21 | try { |
| 22 | body = (await req.json()) as { id?: unknown }; |
| 23 | } catch { |
| 24 | return NextResponse.json({ error: 'invalid json' }, { status: 400 }); |
| 25 | } |
| 26 | const id = typeof body.id === 'string' ? body.id.trim() : ''; |
| 27 | if (!id) return NextResponse.json({ error: 'id required' }, { status: 400 }); |
| 28 | |
| 29 | try { |
| 30 | await deleteImage(id); |
| 31 | console.log(`[admin/images/delete] ${user.email} deleted ${id}`); |
| 32 | return NextResponse.json({ ok: true, id }); |
| 33 | } catch (e) { |
| 34 | const msg = e instanceof Error ? e.message : 'delete failed'; |
| 35 | console.error('[admin/images/delete]', msg); |
| 36 | return NextResponse.json({ error: msg }, { status: 502 }); |
| 37 | } |
| 38 | } |