/** * Create a Hub login (or reset its password and two-factor). * * npm run create-admin -- --email you@example.com --name "Your Name" * * Asks for the password on the terminal. Two-factor is set up at the first login. */ require('dotenv').config(); const readline = require('readline'); const bcrypt = require('bcryptjs'); const prisma = require('../src/config/db'); function arg(name) { const i = process.argv.indexOf(`--${name}`); return i > -1 ? process.argv[i + 1] : null; } function ask(question) { const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); return new Promise(resolve => rl.question(question, answer => { rl.close(); resolve(answer); })); } (async () => { const email = String(arg('email') || '').trim().toLowerCase(); const name = arg('name') || email; if (!email) throw new Error('Usage: npm run create-admin -- --email you@example.com --name "Your Name"'); const password = process.env.HUB_ADMIN_PASSWORD || await ask('Password (12+ characters): '); if (String(password).length < 12) throw new Error('Use at least 12 characters.'); const hash = await bcrypt.hash(password, 12); const admin = await prisma.adminUser.upsert({ where: { email }, update: { password: hash, name, totpEnabled: false, totpSecret: null, tokenVersion: { increment: 1 } }, create: { email, name, password: hash }, }); console.log(`Saved ${admin.email}. Log in to the Hub to set up two-factor.`); process.exit(0); })().catch(e => { console.error(e.message); process.exit(1); });