@objectstack/plugin-auth 7.5.0 → 7.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -1,4 +1,5 @@
1
1
  // src/auth-plugin.ts
2
+ import { SystemObjectName as SystemObjectName3, SystemUserId } from "@objectstack/spec/system";
2
3
  import {
3
4
  SETUP_APP,
4
5
  SETUP_NAV_CONTRIBUTIONS,
@@ -1528,6 +1529,9 @@ var AuthPlugin = class {
1528
1529
  }
1529
1530
  });
1530
1531
  }
1532
+ ctx.hook("kernel:ready", async () => {
1533
+ await this.maybeSeedDevAdmin(ctx);
1534
+ });
1531
1535
  try {
1532
1536
  const ql = ctx.getService("objectql");
1533
1537
  if (ql && typeof ql.registerMiddleware === "function") {
@@ -1547,6 +1551,66 @@ var AuthPlugin = class {
1547
1551
  async destroy() {
1548
1552
  this.authManager = null;
1549
1553
  }
1554
+ /**
1555
+ * Dev-only admin bootstrap.
1556
+ *
1557
+ * On an EMPTY database (zero users), provision a well-known, loginable
1558
+ * admin (admin@objectos.ai / admin123 by default) so backend debugging
1559
+ * never blocks on a first-run sign-up wizard. The account is created
1560
+ * through better-auth's real server-side `signUpEmail` pipeline (hashed
1561
+ * credential + the same hooks the HTTP endpoint runs), so it is fully
1562
+ * loginable; plugin-security's first-user middleware then promotes it to
1563
+ * platform admin automatically.
1564
+ *
1565
+ * This replaces two earlier, divergent seeds:
1566
+ * • the CLI-side HTTP seed (`os dev`), which POSTed the public sign-up
1567
+ * endpoint from the parent process — racing server readiness and
1568
+ * targeting a hard-coded port that broke under dev port auto-shift; and
1569
+ * • plugin-dev's raw `sys_user` insert, which produced a credential-less,
1570
+ * un-loginable row.
1571
+ * Running it in-process needs no port and no readiness polling.
1572
+ *
1573
+ * Idempotent and non-destructive: it only ever acts on a zero-user DB and
1574
+ * never touches an existing account, so a custom password is never
1575
+ * overwritten.
1576
+ *
1577
+ * HARD-GATED to development (NODE_ENV==='development'): a known-credential
1578
+ * admin can never be provisioned in production. Opt out within dev via
1579
+ * OS_SEED_ADMIN=0 (or false/off/no).
1580
+ */
1581
+ async maybeSeedDevAdmin(ctx) {
1582
+ if (process.env.NODE_ENV !== "development") return;
1583
+ const flag = String(process.env.OS_SEED_ADMIN ?? "").trim().toLowerCase();
1584
+ if (["0", "false", "off", "no"].includes(flag)) return;
1585
+ const email = process.env.OS_SEED_ADMIN_EMAIL?.trim() || "admin@objectos.ai";
1586
+ const password = process.env.OS_SEED_ADMIN_PASSWORD?.trim() || "admin123";
1587
+ const name = process.env.OS_SEED_ADMIN_NAME?.trim() || "Dev Admin";
1588
+ let ql;
1589
+ try {
1590
+ ql = ctx.getService("objectql");
1591
+ } catch {
1592
+ }
1593
+ if (!ql || typeof ql.find !== "function") return;
1594
+ try {
1595
+ const rows = await ql.find(SystemObjectName3.USER, { where: {}, limit: 50 }, { context: { isSystem: true } }).catch(() => []);
1596
+ const humans = (Array.isArray(rows) ? rows : []).filter((u) => u && u.id !== SystemUserId.SYSTEM && u.role !== "system");
1597
+ if (humans.length > 0) {
1598
+ ctx.logger.debug("[auth] dev admin seed skipped \u2014 a user already exists");
1599
+ return;
1600
+ }
1601
+ if (!this.authManager) return;
1602
+ const api = await this.authManager.getApi();
1603
+ if (typeof api?.signUpEmail !== "function") {
1604
+ ctx.logger.warn("[auth] dev admin seed skipped \u2014 signUpEmail unavailable");
1605
+ return;
1606
+ }
1607
+ await api.signUpEmail({ body: { email, password, name } });
1608
+ ctx.logger.info(`\u{1F511} Dev admin seeded: ${email} / ${password}`);
1609
+ this.authManager.devSeedResult = { email, password };
1610
+ } catch (err) {
1611
+ ctx.logger.warn(`[auth] dev admin seed skipped: ${err?.message ?? err}`);
1612
+ }
1613
+ }
1550
1614
  /**
1551
1615
  * Register authentication routes with HTTP server
1552
1616
  *