fix: allow seed-prod without SEED_ADMIN_KEY when DB is empty (bootstrap mode)

This commit is contained in:
Z User committed 2026-10-28 21:48:38 +00:00
1 parent b5d3df6d7c
commit 2251209935
1 file changed
+17 -4
+17 -4
View File
@@ -15,15 +15,28 @@ export const maxDuration = 120
// curl -X POST https://dfmpanel.eu/api/aos/seed-prod \ // curl -X POST https://dfmpanel.eu/api/aos/seed-prod \
// -H "X-Seed-Key: $SEED_ADMIN_KEY" // -H "X-Seed-Key: $SEED_ADMIN_KEY"
export async function POST(req: Request) { export async function POST(req: Request) {
// Auth via shared secret (header or query param) // Auth: if SEED_ADMIN_KEY is set, require it. If NOT set, allow seeding
// only when the database is empty (first-time bootstrap — no auth needed
// because there's nothing to protect yet).
const seedKey = process.env.SEED_ADMIN_KEY const seedKey = process.env.SEED_ADMIN_KEY
if (!seedKey) {
return NextResponse.json({ error: 'SEED_ADMIN_KEY env not configured' }, { status: 503 })
}
const providedKey = req.headers.get('x-seed-key') ?? new URL(req.url).searchParams.get('key') const providedKey = req.headers.get('x-seed-key') ?? new URL(req.url).searchParams.get('key')
if (seedKey) {
// Auth mode: key must match
if (providedKey !== seedKey) { if (providedKey !== seedKey) {
return NextResponse.json({ error: 'unauthorized' }, { status: 401 }) return NextResponse.json({ error: 'unauthorized' }, { status: 401 })
} }
} else {
// Bootstrap mode: only allowed when DB is empty (companyCount === 0)
const { db } = await import('@/lib/db')
const companyCount = await db.company.count()
if (companyCount > 0) {
return NextResponse.json({
error: 'SEED_ADMIN_KEY not configured and DB is not empty. Set SEED_ADMIN_KEY env to seed a non-empty database.',
companyCount,
}, { status: 403 })
}
}
try { try {
const stats: Record<string, number> = {} const stats: Record<string, number> = {}