feature: env vars to auto-create root user during initial setup

This commit is contained in:
alex-sviridov 2025-09-28 22:17:12 +02:00
parent 03da194953
commit 0a49d13c67
3 changed files with 33 additions and 1 deletions

View file

@ -160,6 +160,9 @@ class Server {
// Create or set JWT secret in token manager
await this.auth.tokenManager.initTokenSecret()
// Auto-create root user if environment variables are set and no root user exists
await this.autoCreateRootUser()
await Logger.logManager.init()
await this.cleanUserData() // Remove invalid user item progress
@ -428,6 +431,30 @@ class Server {
SocketAuthority.initialize(this)
}
async autoCreateRootUser() {
if (!Database.hasRootUser && process.env.INIT_USER_NAME) {
const initUsername = process.env.INIT_USER_NAME
const initPassword = process.env.INIT_USER_PASSWORD || ''
Logger.info(`[Server] Auto-creating root user "${initUsername}" from environment variables`)
try {
const hashedPassword = initPassword ? await this.auth.localAuthStrategy.hashPassword(initPassword) : ''
if (!hashedPassword && initPassword) {
Logger.warn(`[Server] Failed to hash password for auto-created root user`)
} else if (!initPassword) {
Logger.warn(`[Server] Creating root user with no password`)
}
await Database.createRootUser(initUsername, hashedPassword, this.auth)
Logger.info(`[Server] Successfully auto-created root user "${initUsername}"`)
} catch (error) {
Logger.error(`[Server] Failed to auto-create root user:`, error)
}
}
}
async initializeServer(req, res) {
Logger.info(`[Server] Initializing new server`)
const newRoot = req.body.newRoot