Add seed script to create first admin from env
This commit is contained in:
@@ -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}".`);
|
||||||
Reference in New Issue
Block a user