Branchwell

Lumen & Gesso / website · lumengesso.com / app / api / admin / images / delete / route.ts

1import { NextResponse, type NextRequest } from 'next/server';
2import { getCustomerUser } from '@/app/lib/lumen-auth';
3import { isAdmin } from '@/app/lib/admin';
4import { deleteImage } from '@/app/lib/cf-images';
5
6export const runtime = 'nodejs';
7export 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.
14export 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}