fb4880a1d9
Adds an authenticated admin surface gated by auth.users.app_metadata.role==='admin'. - lib/auth/admin-guard.ts: requireAdmin() (pages) + requireAdminApi() (routes) - middleware.ts: defense-in-depth /admin and /api/admin guarding - API: users (list/detail/role/ban/delete), tunnels (list + active/quota/reset/reassign/regenerate-token/delete), metrics, audit log, reserved subdomains - Self-lockout prevention (no self demote/ban/delete) - Best-effort Redis kill-switch via dependency-free net-socket client (REDIS_URL) - admin_audit_log + reserved_subdomains migration (RLS on, service-role only) - Admin UI (overview, users, tunnels, reserved, audit) + conditional nav link
49 lines
1.4 KiB
TypeScript
49 lines
1.4 KiB
TypeScript
import { NextResponse, type NextRequest } from 'next/server';
|
|
import { requireAdminApi } from '@/lib/auth/admin-guard';
|
|
import { getSupabaseAdmin } from '@/lib/supabase/admin';
|
|
import { logAdminAction } from '@/lib/auth/audit';
|
|
import { isUuid } from '@/lib/admin/validators';
|
|
import { redisSet } from '@/lib/redis';
|
|
|
|
export const runtime = 'nodejs';
|
|
export const dynamic = 'force-dynamic';
|
|
|
|
export async function DELETE(
|
|
_req: NextRequest,
|
|
{ params }: { params: { id: string } },
|
|
) {
|
|
const auth = await requireAdminApi();
|
|
if (!auth.ok) return auth.response;
|
|
|
|
const { id } = params;
|
|
if (!isUuid(id)) {
|
|
return NextResponse.json({ error: 'invalid tunnel id' }, { status: 400 });
|
|
}
|
|
|
|
const admin = getSupabaseAdmin();
|
|
const { data, error } = await admin
|
|
.from('tunnels')
|
|
.delete()
|
|
.eq('id', id)
|
|
.select('subdomain')
|
|
.maybeSingle<{ subdomain: string }>();
|
|
if (error) {
|
|
return NextResponse.json({ error: error.message }, { status: 500 });
|
|
}
|
|
if (!data) {
|
|
return NextResponse.json({ error: 'tunnel not found' }, { status: 404 });
|
|
}
|
|
|
|
// Best-effort live kill-switch.
|
|
await redisSet(`tunnel:active:${data.subdomain}`, '0');
|
|
|
|
await logAdminAction(auth.user, {
|
|
action: 'tunnel.delete',
|
|
target_type: 'tunnel',
|
|
target_id: id,
|
|
details: { subdomain: data.subdomain },
|
|
});
|
|
|
|
return NextResponse.json({ ok: true });
|
|
}
|