From 072b74cd99fa4ddbe8bad63e57ae47056d8460c7 Mon Sep 17 00:00:00 2001 From: jessikitty Date: Wed, 3 Jun 2026 09:51:37 +1000 Subject: [PATCH] Add seed script to create first admin from env --- server/seed.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 server/seed.js 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}".`);