cauthy-auth 0.24.3 → 0.26.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.js CHANGED
@@ -681,6 +681,13 @@ function checkName(value) {
681
681
  if (typeof value !== "string" || value.trim().length > MAX_NAME) return { field: "name", code: "too_long", message: `Use at most ${MAX_NAME} characters.` };
682
682
  return null;
683
683
  }
684
+ var MAX_ORGANIZATION = 120;
685
+ function checkOrganization(value, required = false) {
686
+ const v = typeof value === "string" ? value.trim() : "";
687
+ if (v === "") return required ? { field: "organization", code: "required", message: "Enter your company's name." } : null;
688
+ if (v.length > MAX_ORGANIZATION) return { field: "organization", code: "too_long", message: `Use at most ${MAX_ORGANIZATION} characters.` };
689
+ return null;
690
+ }
684
691
  function checkCode(value) {
685
692
  const v = typeof value === "string" ? value.trim() : "";
686
693
  if (v === "") return { field: "code", code: "required", message: "Enter the code." };
@@ -1236,6 +1243,7 @@ function createCauthy(given = {}) {
1236
1243
  "/me/mfa/phone/remove",
1237
1244
  "/sign-in/mfa/methods",
1238
1245
  "/sign-in/mfa/send",
1246
+ "/sign-in/mfa/email",
1239
1247
  "/sign-in/mfa/push",
1240
1248
  "/sign-in/mfa/push/events",
1241
1249
  "/sign-in/mfa/push/finish",
@@ -1290,6 +1298,8 @@ function createCauthy(given = {}) {
1290
1298
  return json({
1291
1299
  providers: list.map((p) => ({ id: String(p.provider ?? p.id ?? ""), name: String(p.label ?? p.name ?? p.provider ?? "") })).filter((p) => p.id !== ""),
1292
1300
  signUp: settings.data?.allow_signup !== false,
1301
+ // The application's system makes an organization per sign-up: the form asks the company's name.
1302
+ ...settings.data?.signup?.organization?.ask === true ? { organization: { ask: true, required: settings.data.signup.organization.required === true } } : {},
1293
1303
  // A sign-in link needs a page of the application to open.
1294
1304
  magicLink: settings.data?.allow_magic_link === true && pages.signIn !== void 0,
1295
1305
  passkey: place.error ? true : here || place.data?.on_cauthy === true,
@@ -1527,7 +1537,8 @@ function createCauthy(given = {}) {
1527
1537
  if (res.error) return problem(res.status, res.error.code, res.error.message);
1528
1538
  if (res.data?.mfa_required === true) {
1529
1539
  const step = { kind: "mfa", token: String(res.data.mfa_token ?? ""), returnTo: afterSignIn };
1530
- return json({ status: "mfa_required" }, 200, new Headers({ "set-cookie": writeCookie(stepCookie, await seal(step, await key), attrs(request, STEP_SECONDS)) }));
1540
+ const how = res.data.mfa_method === "email" ? { method: "email" } : {};
1541
+ return json({ status: "mfa_required", ...how }, 200, new Headers({ "set-cookie": writeCookie(stepCookie, await seal(step, await key), attrs(request, STEP_SECONDS)) }));
1531
1542
  }
1532
1543
  return signedIn(request, res.data);
1533
1544
  }
@@ -1554,7 +1565,7 @@ function createCauthy(given = {}) {
1554
1565
  if (res.error) return problem(res.status, res.error.code, res.error.message);
1555
1566
  return signedIn(request, res.data, [writeCookie(stepCookie, "", attrs(request, 0))]);
1556
1567
  }
1557
- if (route === "/sign-in/mfa/methods" || route === "/sign-in/mfa/send") {
1568
+ if (route === "/sign-in/mfa/methods" || route === "/sign-in/mfa/send" || route === "/sign-in/mfa/email") {
1558
1569
  const sealed = readCookies(request.headers.get("cookie")).get(stepCookie);
1559
1570
  const step = sealed ? await unseal(sealed, await key) : null;
1560
1571
  if (step === null || step.kind !== "mfa") return problem(400, "step_expired", "Sign in again: the code was asked for too long ago.");
@@ -1563,22 +1574,30 @@ function createCauthy(given = {}) {
1563
1574
  if (res2.error) return problem(res2.status, res2.error.code, res2.error.message);
1564
1575
  const p = res2.data?.phone;
1565
1576
  const push = res2.data?.push;
1577
+ const mail = res2.data?.email;
1566
1578
  return json({
1567
1579
  status: "ok",
1568
1580
  totp: res2.data?.totp === true,
1569
1581
  phone: p ? { number: String(p.number ?? ""), channel: String(p.channel ?? "sms") } : null,
1570
- push: push ? { devices: Number(push.devices ?? 0) } : null
1582
+ push: push ? { devices: Number(push.devices ?? 0) } : null,
1583
+ email: mail ? { to: String(mail.to ?? "") } : null
1571
1584
  });
1572
1585
  }
1586
+ if (route === "/sign-in/mfa/email") {
1587
+ const res2 = await ask(request, "POST", "/v1/auth/mfa/email", { mfa_token: step.token });
1588
+ if (res2.error) return problem(res2.status, res2.error.code, res2.error.message);
1589
+ return json({ status: "sent", sentTo: String(res2.data?.sent_to ?? "") });
1590
+ }
1573
1591
  const res = await ask(request, "POST", "/v1/auth/mfa/send", { mfa_token: step.token });
1574
1592
  if (res.error) return problem(res.status, res.error.code, res.error.message);
1575
1593
  return json({ status: "sent", sentTo: String(res.data?.sent_to ?? ""), channel: String(res.data?.channel ?? "sms") });
1576
1594
  }
1577
1595
  if (route === "/sign-up/email") {
1578
- const bad = firstProblem(checkName(b.name), checkEmail(b.email), checkNewPassword(b.password));
1596
+ const bad = firstProblem(checkName(b.name), checkEmail(b.email), checkNewPassword(b.password), checkOrganization(b.organization));
1579
1597
  if (bad) return invalid(bad);
1580
1598
  const email = String(b.email).trim();
1581
- const made = await ask(request, "POST", "/v1/auth/register", { email, password: b.password, display_name: typeof b.name === "string" ? b.name.trim() : "" }, pages.verifyEmail);
1599
+ const organization = typeof b.organization === "string" && b.organization.trim() !== "" ? { organization: { name: b.organization.trim() } } : {};
1600
+ const made = await ask(request, "POST", "/v1/auth/register", { email, password: b.password, display_name: typeof b.name === "string" ? b.name.trim() : "", ...organization }, pages.verifyEmail);
1582
1601
  if (made.error) return problem(made.status, made.error.code, made.error.message);
1583
1602
  if (made.data?.verification_required === true) return json({ status: "verification_required", email });
1584
1603
  const res = await ask(request, "POST", "/v1/auth/login", { email, password: b.password });
@@ -1780,6 +1799,7 @@ function createCauthy(given = {}) {
1780
1799
  if (!isToken(b.token)) return badLink();
1781
1800
  const res = await ask(request, "POST", "/v1/auth/email/verify", { token: String(b.token) });
1782
1801
  if (res.error) return problem(res.status, res.error.code, res.error.message);
1802
+ if (res.data?.access_token) return signedIn(request, res.data);
1783
1803
  return json({ status: "verified" });
1784
1804
  }
1785
1805
  if (route === "/sign-in/passkey/begin") {