diff --git a/server/seed.js b/server/seed.js new file mode 100644 index 0000000..9f7d350 --- /dev/null +++ b/server/seed.js @@ -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}".`);