25 lines
850 B
JavaScript
25 lines
850 B
JavaScript
import 'dotenv/config';
|
|
import bcrypt from 'bcryptjs';
|
|
import { db } from './db.js';
|
|
|
|
const username = process.env.ADMIN_USERNAME || 'admin';
|
|
const password = process.env.ADMIN_PASSWORD;
|
|
if (!password || password.length < 12) {
|
|
console.error('FATAL: ADMIN_PASSWORD env var not set or shorter than 12 chars.');
|
|
process.exit(1);
|
|
}
|
|
|
|
const existing = db.prepare('SELECT id FROM users WHERE username = ?').get(username);
|
|
if (existing) {
|
|
console.log(`User "${username}" already exists (id=${existing.id}). Skipping.`);
|
|
process.exit(0);
|
|
}
|
|
|
|
const hash = bcrypt.hashSync(password, 10);
|
|
const info = db
|
|
.prepare('INSERT INTO users (username, password_hash) VALUES (?, ?)')
|
|
.run(username, hash);
|
|
|
|
console.log(`Created admin user "${username}" (id=${info.lastInsertRowid}).`);
|
|
console.log(`Login with the password from your .env (ADMIN_PASSWORD).`);
|