Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ node_modules/
resources/fonts/*

# Local share store (dev only — production uses Vercel KV)
shares/
/shares/
44 changes: 44 additions & 0 deletions app/api/admin/shares/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { NextRequest } from 'next/server';
import { listShares, deleteShare } from '@/lib/share-store';

const ID_RE = /^[0-9a-f]{16}$/;

function checkAuth(req: NextRequest): 'ok' | 'missing' | 'unauthorized' {
const secret = process.env.ADMIN_SECRET;
if (!secret) return 'missing';
return req.headers.get('authorization') === `Bearer ${secret}` ? 'ok' : 'unauthorized';
}

export async function GET(req: NextRequest) {
const auth = checkAuth(req);
if (auth === 'missing') return Response.json({ error: 'Admin not configured' }, { status: 503 });
if (auth === 'unauthorized') return Response.json({ error: 'Unauthorized' }, { status: 401 });

const ids = await listShares();
const proto = req.headers.get('x-forwarded-proto') ?? 'http';
const host = req.headers.get('host') ?? 'localhost:3000';
const base = process.env.APP_URL ?? `${proto}://${host}`;
const shares = ids.map((id) => ({ id, view: `${base}/view/${id}` }));
return Response.json({ count: shares.length, shares });
}

export async function DELETE(req: NextRequest) {
const auth = checkAuth(req);
if (auth === 'missing') return Response.json({ error: 'Admin not configured' }, { status: 503 });
if (auth === 'unauthorized') return Response.json({ error: 'Unauthorized' }, { status: 401 });

let body: { id?: string };
try {
body = await req.json();
} catch {
return Response.json({ error: 'Invalid body' }, { status: 400 });
}

const { id } = body;
if (!id || !ID_RE.test(id)) {
return Response.json({ error: 'Invalid id (expected 16 hex chars)' }, { status: 400 });
}

await deleteShare(id);
return Response.json({ deleted: true, id });
}
Loading