feat: add superadmin management
This commit is contained in:
@@ -3,20 +3,22 @@ import { config } from '../config.js';
|
||||
import { closePool, pool } from './pool.js';
|
||||
|
||||
async function bootstrapAdmin() {
|
||||
if (!config.BOOTSTRAP_ADMIN_EMAIL || !config.BOOTSTRAP_ADMIN_PASSWORD) {
|
||||
throw new Error('Set BOOTSTRAP_ADMIN_EMAIL and BOOTSTRAP_ADMIN_PASSWORD before running this command.');
|
||||
if (!config.SUPERADMIN_EMAIL || !config.SUPERADMIN_PASSWORD) {
|
||||
console.warn('SUPERADMIN_EMAIL and SUPERADMIN_PASSWORD are not set; skipping superadmin bootstrap.');
|
||||
return;
|
||||
}
|
||||
|
||||
const passwordHash = await hashPassword(config.BOOTSTRAP_ADMIN_PASSWORD);
|
||||
const passwordHash = await hashPassword(config.SUPERADMIN_PASSWORD);
|
||||
const result = await pool.query<{ email: string }>(
|
||||
`insert into users (email, password_hash, display_name, role)
|
||||
values ($1, $2, $3, 'admin')
|
||||
on conflict (email) do update
|
||||
set password_hash = excluded.password_hash,
|
||||
display_name = excluded.display_name,
|
||||
role = 'admin'
|
||||
role = 'admin',
|
||||
is_active = true
|
||||
returning email`,
|
||||
[config.BOOTSTRAP_ADMIN_EMAIL, passwordHash, config.BOOTSTRAP_ADMIN_NAME],
|
||||
[config.SUPERADMIN_EMAIL, passwordHash, config.SUPERADMIN_NAME],
|
||||
);
|
||||
console.log(`Administrator ready: ${result.rows[0].email}`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user