Add seed script to create first admin from env

This commit is contained in:
2026-06-03 09:51:37 +10:00
parent 0286d10e57
commit 072b74cd99
+21
View File
@@ -0,0 +1,21 @@
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}".`);