SPB Git
2.5 KB · 23 lines typescript
1import "./_env";2import postgres from "postgres";3import { hashPassword } from "../src/lib/auth/password";4/** Creates the owner account from OWNER_EMAIL / OWNER_INITIAL_PASSWORD if no user exists; idempotent. Also seeds default settings. */5const sql = postgres(process.env.DATABASE_URL!, { max: 1, onnotice: () => {} });6const email = process.env.OWNER_EMAIL, pw = process.env.OWNER_INITIAL_PASSWORD;7const [{ n }] = await sql`SELECT count(*)::int AS n FROM users`;8if (n === 0) {9  if (!email || !pw) { console.error("OWNER_EMAIL / OWNER_INITIAL_PASSWORD missing"); process.exit(1); }10  await sql`INSERT INTO users (email, display_name, password_hash, role, must_change_password) VALUES (${email.toLowerCase()}, ${"Owner"}, ${await hashPassword(pw)}, 'owner', true)`;11  await sql`INSERT INTO audit_log (actor_label, action, entity_type, entity_id, after) VALUES ('bootstrap', 'user.create', 'user', ${email.toLowerCase()}, ${sql.json({ role: "owner" })})`;12  console.log(`owner ${email} created (must change password at first login)`);13} else console.log(`users present (${n}); nothing to do`);14const defaults: Record<string, unknown> = {15  "site.attribution": { en: "This observatory is an independent publication portal maintained by Simon-Pierre Boucher, professor at the Université du Québec en Outaouais (Département des sciences administratives). It is not an official UQO institutional repository. Working papers are circulated for discussion and comment; they have not necessarily undergone formal peer review.", fr: "Cet observatoire est un portail de publication indépendant maintenu par Simon-Pierre Boucher, professeur à l'Université du Québec en Outaouais (Département des sciences administratives). Ce n'est pas un dépôt institutionnel officiel de l'UQO. Les cahiers de recherche circulent aux fins de discussion et de commentaires ; ils n'ont pas nécessairement fait l'objet d'une évaluation par les pairs." },16  "site.institution": { name_en: "Université du Québec en Outaouais", name_fr: "Université du Québec en Outaouais", department_en: "Département des sciences administratives", department_fr: "Département des sciences administratives", url: "https://uqo.ca", logo_authorized: false },17  "site.contact": { email: "simon-pierre.boucher@uqo.ca", website: "https://www.spboucher.ai" },18  "home.featured_limit": 4,19  "ingest.extra_dirs": [],20};21for (const [k, v] of Object.entries(defaults)) await sql`INSERT INTO settings (key, value) VALUES (${k}, ${sql.json(v as never)}) ON CONFLICT (key) DO NOTHING`;22await sql.end();23
Files · ba542514eac709a3e76ac3b3f88404c61356d594
Menu

Theme