cauthy-auth 0.24.2 → 0.25.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
@@ -633,6 +633,13 @@ function checkName(value) {
633
633
  if (typeof value !== "string" || value.trim().length > MAX_NAME) return { field: "name", code: "too_long", message: `Use at most ${MAX_NAME} characters.` };
634
634
  return null;
635
635
  }
636
+ var MAX_ORGANIZATION = 120;
637
+ function checkOrganization(value, required = false) {
638
+ const v = typeof value === "string" ? value.trim() : "";
639
+ if (v === "") return required ? { field: "organization", code: "required", message: "Enter your company's name." } : null;
640
+ if (v.length > MAX_ORGANIZATION) return { field: "organization", code: "too_long", message: `Use at most ${MAX_ORGANIZATION} characters.` };
641
+ return null;
642
+ }
636
643
  function checkCode(value) {
637
644
  const v = typeof value === "string" ? value.trim() : "";
638
645
  if (v === "") return { field: "code", code: "required", message: "Enter the code." };
@@ -1097,10 +1104,11 @@ function createCauthy(given = {}) {
1097
1104
  const clientIp = given.clientIp ?? ((request) => request.headers.get("x-forwarded-for")?.split(",").pop()?.trim() || void 0);
1098
1105
  let noProofUntil = 0;
1099
1106
  async function passedOn(request) {
1107
+ if (Date.now() < noProofUntil) return {};
1100
1108
  const ip = clientIp(request);
1101
- if (!ip || Date.now() < noProofUntil) return {};
1102
1109
  try {
1103
- return { "x-cauthy-client-ip": ip, "x-cauthy-system-token": await ownSystem.token() };
1110
+ const proof = { "x-cauthy-system-token": await ownSystem.token() };
1111
+ return ip ? { ...proof, "x-cauthy-client-ip": ip } : proof;
1104
1112
  } catch {
1105
1113
  noProofUntil = Date.now() + 5 * 6e4;
1106
1114
  return {};
@@ -1241,6 +1249,8 @@ function createCauthy(given = {}) {
1241
1249
  return json({
1242
1250
  providers: list.map((p) => ({ id: String(p.provider ?? p.id ?? ""), name: String(p.label ?? p.name ?? p.provider ?? "") })).filter((p) => p.id !== ""),
1243
1251
  signUp: settings.data?.allow_signup !== false,
1252
+ // The application's system makes an organization per sign-up: the form asks the company's name.
1253
+ ...settings.data?.signup?.organization?.ask === true ? { organization: { ask: true, required: settings.data.signup.organization.required === true } } : {},
1244
1254
  // A sign-in link needs a page of the application to open.
1245
1255
  magicLink: settings.data?.allow_magic_link === true && pages.signIn !== void 0,
1246
1256
  passkey: place.error ? true : here || place.data?.on_cauthy === true,
@@ -1526,10 +1536,11 @@ function createCauthy(given = {}) {
1526
1536
  return json({ status: "sent", sentTo: String(res.data?.sent_to ?? ""), channel: String(res.data?.channel ?? "sms") });
1527
1537
  }
1528
1538
  if (route === "/sign-up/email") {
1529
- const bad = firstProblem(checkName(b.name), checkEmail(b.email), checkNewPassword(b.password));
1539
+ const bad = firstProblem(checkName(b.name), checkEmail(b.email), checkNewPassword(b.password), checkOrganization(b.organization));
1530
1540
  if (bad) return invalid(bad);
1531
1541
  const email = String(b.email).trim();
1532
- const made = await ask(request, "POST", "/v1/auth/register", { email, password: b.password, display_name: typeof b.name === "string" ? b.name.trim() : "" }, pages.verifyEmail);
1542
+ const organization = typeof b.organization === "string" && b.organization.trim() !== "" ? { organization: { name: b.organization.trim() } } : {};
1543
+ 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);
1533
1544
  if (made.error) return problem(made.status, made.error.code, made.error.message);
1534
1545
  if (made.data?.verification_required === true) return json({ status: "verification_required", email });
1535
1546
  const res = await ask(request, "POST", "/v1/auth/login", { email, password: b.password });
@@ -1731,6 +1742,7 @@ function createCauthy(given = {}) {
1731
1742
  if (!isToken(b.token)) return badLink();
1732
1743
  const res = await ask(request, "POST", "/v1/auth/email/verify", { token: String(b.token) });
1733
1744
  if (res.error) return problem(res.status, res.error.code, res.error.message);
1745
+ if (res.data?.access_token) return signedIn(request, res.data);
1734
1746
  return json({ status: "verified" });
1735
1747
  }
1736
1748
  if (route === "/sign-in/passkey/begin") {