22 lines
665 B
JavaScript
22 lines
665 B
JavaScript
import "dotenv/config";
|
|
import bcrypt from "bcryptjs";
|
|
import db from "./db.js";
|
|
|
|
const username = process.env.ADMIN_USER;
|
|
const password = process.env.ADMIN_PASS;
|
|
|
|
if (!username || !password) {
|
|
console.error("Set ADMIN_USER and ADMIN_PASS in .env before seeding.");
|
|
process.exit(1);
|
|
}
|
|
|
|
const existing = db.prepare("SELECT id FROM admins WHERE username = ?").get(username);
|
|
if (existing) {
|
|
console.log(`Admin "${username}" already exists — nothing to do.`);
|
|
process.exit(0);
|
|
}
|
|
|
|
const hash = bcrypt.hashSync(password, 12);
|
|
db.prepare("INSERT INTO admins (username, pw_hash) VALUES (?, ?)").run(username, hash);
|
|
console.log(`Created admin "${username}".`);
|