Repository Public
1// Invitation codes for account creation (used when OPEN_REGISTRATION=false).2// node scripts/invite.ts create [note] → prints a one-time code (only its hash is stored)3// node scripts/invite.ts list4import { randomBytes } from "node:crypto";5import { loadConfig } from "../src/config.ts";6import { openDatabase } from "../src/db.ts";7import { sha256 } from "../src/util.ts";89const config = loadConfig();10const db = openDatabase(config.dbPath);11const [command, ...rest] = process.argv.slice(2);12if (command === "create") {13 const alphabet = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";14 const raw = [...randomBytes(16)].map((b) => alphabet[b % alphabet.length]).join("");15 db.prepare("INSERT INTO invites(code_hash, note, created_at) VALUES(?,?,?)").run(sha256(raw), rest.join(" "), Date.now());16 console.log(raw.replace(/(.{4})(?=.)/g, "$1-"));17} else if (command === "list") {18 for (const row of db.prepare("SELECT note, created_at, used_at FROM invites ORDER BY created_at DESC").all() as any[]) {19 console.log(`${new Date(row.created_at).toISOString()} ${row.used_at ? "used " + new Date(row.used_at).toISOString() : "unused"} ${row.note}`);20 }21} else {22 console.error("usage: node scripts/invite.ts create [note] | list");23 process.exit(2);24}25