feat: add control plane authentication baseline

This commit is contained in:
wlx committed 2026-08-21 15:47:57 +08:00
1 parent a4c93ff1a7
commit 0d10e2d124
21 files changed
+1403 -18

No files matched your search

+26
View File
@@ -0,0 +1,26 @@
import { AuthService } from './auth.js'
import { PostgresAuthRepository } from './postgres.js'
const databaseUrl = process.env.DATABASE_URL
if (!databaseUrl) throw new Error('DATABASE_URL is required')
const repository = new PostgresAuthRepository(databaseUrl)
const auth = new AuthService(repository, {
sessionTtlHours: Number(process.env.SESSION_TTL_HOURS || 168),
passwordPepper: process.env.AUTH_PASSWORD_PEPPER || 'development-only-pepper',
})
await auth.ensureAdmin(
process.env.ADMIN_ACCOUNT || 'admin@moyi.local',
requiredEnvironment('ADMIN_PASSWORD'),
process.env.ADMIN_DISPLAY_NAME || '平台管理员',
)
await repository.close()
console.log(`Admin account ensured: ${process.env.ADMIN_ACCOUNT || 'admin@moyi.local'}`)
function requiredEnvironment(name: string) {
const value = process.env[name]
if (!value || value.startsWith('replace-with-')) throw new Error(`${name} must be set to a non-placeholder value`)
return value
}