39 lines
1.3 KiB
JavaScript
39 lines
1.3 KiB
JavaScript
#!/usr/bin/env node
|
|
import readline from 'node:readline/promises';
|
|
import { stdin as input, stdout as output } from 'node:process';
|
|
import { createDbPool, isDatabaseConfigured } from '../server/db.mjs';
|
|
import { createLocalUserAuth } from '../server/local-auth.mjs';
|
|
|
|
if (!isDatabaseConfigured()) {
|
|
console.error('MySQL 未配置,请先设置 DATABASE_URL 或 MYSQL_*');
|
|
process.exit(1);
|
|
}
|
|
|
|
const rl = readline.createInterface({ input, output });
|
|
const username = (await rl.question('admin username [admin]: ')).trim() || 'admin';
|
|
const password = await rl.question('admin password: ');
|
|
const confirm = await rl.question('confirm password: ');
|
|
rl.close();
|
|
|
|
if (!password || password !== confirm) {
|
|
console.error('密码为空或两次输入不一致');
|
|
process.exit(1);
|
|
}
|
|
|
|
const pool = createDbPool();
|
|
const auth = createLocalUserAuth(pool);
|
|
await auth.ensureAdminUser().catch(() => {});
|
|
await pool.execute(
|
|
`INSERT INTO auth_users (username, display_name, role, status, password_hash, balance_cents)
|
|
VALUES (?, ?, 'admin', 'active', ?, 0)
|
|
ON DUPLICATE KEY UPDATE
|
|
display_name = VALUES(display_name),
|
|
role = VALUES(role),
|
|
status = VALUES(status),
|
|
password_hash = VALUES(password_hash)`,
|
|
[username, username, auth.hashPassword(password)],
|
|
);
|
|
|
|
console.log(`admin 账号已写入数据库: ${username}`);
|
|
await pool.end();
|