seed-admin: prompt for username (default "admin")
Running `pnpm --filter @parking/server seed-admin` with no env vars now prompts for a username (blank -> "admin") and then a password. Env vars (ADMIN_USER / ADMIN_PASS) and a CLI arg still work for non-interactive installs. Read prompts through a single readline async line-iterator so it's robust over both a TTY and a pipe (chaining readline/promises question() over a pipe could drop buffered lines). Verified: default + custom username, env-var path.
This commit is contained in:
@@ -1,12 +1,13 @@
|
|||||||
// Seed the first admin user (run once at install).
|
// Seed the first admin user (run once at install).
|
||||||
//
|
//
|
||||||
// ADMIN_USER=admin ADMIN_PASS='strong-pass' \
|
// pnpm --filter @parking/server seed-admin
|
||||||
// node --env-file-if-exists=.env apps/server/scripts/seed-admin.mjs
|
// -> prompts for a username (default "admin") and password
|
||||||
//
|
//
|
||||||
// Or interactively (prompts for a hidden password):
|
// Non-interactive (install scripts):
|
||||||
// node --env-file-if-exists=.env apps/server/scripts/seed-admin.mjs <username>
|
// ADMIN_USER=admin ADMIN_PASS='strong-pass' pnpm --filter @parking/server seed-admin
|
||||||
//
|
//
|
||||||
// Idempotent-ish: refuses to overwrite an existing user unless FORCE=1.
|
// A username may also be passed as an argument. Refuses to overwrite an existing
|
||||||
|
// user unless FORCE=1 (which resets that user's password).
|
||||||
|
|
||||||
import { randomUUID } from "node:crypto";
|
import { randomUUID } from "node:crypto";
|
||||||
import { createInterface } from "node:readline/promises";
|
import { createInterface } from "node:readline/promises";
|
||||||
@@ -17,19 +18,35 @@ const require = createRequire(import.meta.url);
|
|||||||
const bcrypt = require("bcrypt");
|
const bcrypt = require("bcrypt");
|
||||||
const { createDb, users, eq } = require("@parking/db");
|
const { createDb, users, eq } = require("@parking/db");
|
||||||
|
|
||||||
const username = process.env.ADMIN_USER ?? process.argv[2];
|
const DEFAULT_USERNAME = "admin";
|
||||||
let password = process.env.ADMIN_PASS;
|
|
||||||
|
|
||||||
|
// Lazily create one readline interface and read answers sequentially through a
|
||||||
|
// single async line iterator — robust whether stdin is a TTY or a pipe (chaining
|
||||||
|
// readline/promises question() over a pipe can drop buffered lines).
|
||||||
|
let rl = null;
|
||||||
|
let lines = null;
|
||||||
|
async function prompt(label) {
|
||||||
|
if (!rl) {
|
||||||
|
rl = createInterface({ input: stdin, output: stdout });
|
||||||
|
lines = rl[Symbol.asyncIterator]();
|
||||||
|
}
|
||||||
|
stdout.write(label);
|
||||||
|
const { value } = await lines.next();
|
||||||
|
return (value ?? "").trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Username: env var > CLI arg > prompt (blank -> default "admin").
|
||||||
|
let username = process.env.ADMIN_USER ?? process.argv[2];
|
||||||
if (!username) {
|
if (!username) {
|
||||||
console.error("usage: ADMIN_USER=.. ADMIN_PASS=.. seed-admin.mjs (or pass a username arg)");
|
username = (await prompt(`Admin username [${DEFAULT_USERNAME}]: `)) || DEFAULT_USERNAME;
|
||||||
process.exit(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let password = process.env.ADMIN_PASS;
|
||||||
if (!password) {
|
if (!password) {
|
||||||
const rl = createInterface({ input: stdin, output: stdout });
|
password = await prompt(`Password for "${username}": `);
|
||||||
password = (await rl.question(`Password for "${username}": `)).trim();
|
|
||||||
rl.close();
|
|
||||||
}
|
}
|
||||||
|
rl?.close();
|
||||||
|
|
||||||
if (!password || password.length < 8) {
|
if (!password || password.length < 8) {
|
||||||
console.error("password must be at least 8 characters");
|
console.error("password must be at least 8 characters");
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
|
|||||||
Reference in New Issue
Block a user