feat: implement file deletion functionality and enhance deployment script

This commit is contained in:
Anders Böttcher
2026-05-13 12:35:58 +02:00
parent cf23467be6
commit 9dbbf814b4
10 changed files with 229 additions and 50 deletions
+19 -7
View File
@@ -152,20 +152,32 @@ export async function countUsers(): Promise<number> {
}
/**
* Seeds an initial admin user if no users exist yet.
* Reads ADMIN_USERNAME (default "admin") and ADMIN_PASSWORD from environment.
* Ensures the admin user defined by ADMIN_USERNAME / ADMIN_PASSWORD env vars
* exists and has the correct password. Runs on every login attempt so that
* changing the env var (and restarting the container) takes effect immediately.
* Does nothing when ADMIN_PASSWORD is not set.
*/
export async function seedAdminIfEmpty(): Promise<void> {
const count = await countUsers()
if (count > 0) return
const password = process.env.ADMIN_PASSWORD
if (!password) return
const username = process.env.ADMIN_USERNAME ?? 'admin'
try {
await createUser(username, password, 'admin')
console.log(`[auth] Seeded initial admin user: "${username}"`)
const existing = await findUserByUsername(username)
if (!existing) {
await createUser(username, password, 'admin')
console.log(`[auth] Created admin user: "${username}"`)
} else {
// Always sync the password so env-var changes take effect after restart
const matches = await verifyPassword(password, existing.passwordHash, existing.salt)
if (!matches) {
await updatePassword(existing.id, password)
console.log(`[auth] Synced password for admin user: "${username}"`)
}
}
} catch (e) {
console.error('[auth] Failed to seed admin user:', e)
console.error('[auth] Failed to sync admin user:', e)
}
}