mirror of
https://gitlab.w-hs.de/an14051/backerup-website.git
synced 2026-07-27 17:35:29 +00:00
feat: implement file deletion functionality and enhance deployment script
This commit is contained in:
+19
-7
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user