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.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." };
@@ -1145,10 +1152,11 @@ function createCauthy(given = {}) {
1145
1152
  const clientIp = given.clientIp ?? ((request) => request.headers.get("x-forwarded-for")?.split(",").pop()?.trim() || void 0);
1146
1153
  let noProofUntil = 0;
1147
1154
  async function passedOn(request) {
1155
+ if (Date.now() < noProofUntil) return {};
1148
1156
  const ip = clientIp(request);
1149
- if (!ip || Date.now() < noProofUntil) return {};
1150
1157
  try {
1151
- return { "x-cauthy-client-ip": ip, "x-cauthy-system-token": await ownSystem.token() };
1158
+ const proof = { "x-cauthy-system-token": await ownSystem.token() };
1159
+ return ip ? { ...proof, "x-cauthy-client-ip": ip } : proof;
1152
1160
  } catch {
1153
1161
  noProofUntil = Date.now() + 5 * 6e4;
1154
1162
  return {};
@@ -1289,6 +1297,8 @@ function createCauthy(given = {}) {
1289
1297
  return json({
1290
1298
  providers: list.map((p) => ({ id: String(p.provider ?? p.id ?? ""), name: String(p.label ?? p.name ?? p.provider ?? "") })).filter((p) => p.id !== ""),
1291
1299
  signUp: settings.data?.allow_signup !== false,
1300
+ // The application's system makes an organization per sign-up: the form asks the company's name.
1301
+ ...settings.data?.signup?.organization?.ask === true ? { organization: { ask: true, required: settings.data.signup.organization.required === true } } : {},
1292
1302
  // A sign-in link needs a page of the application to open.
1293
1303
  magicLink: settings.data?.allow_magic_link === true && pages.signIn !== void 0,
1294
1304
  passkey: place.error ? true : here || place.data?.on_cauthy === true,
@@ -1574,10 +1584,11 @@ function createCauthy(given = {}) {
1574
1584
  return json({ status: "sent", sentTo: String(res.data?.sent_to ?? ""), channel: String(res.data?.channel ?? "sms") });
1575
1585
  }
1576
1586
  if (route === "/sign-up/email") {
1577
- const bad = firstProblem(checkName(b.name), checkEmail(b.email), checkNewPassword(b.password));
1587
+ const bad = firstProblem(checkName(b.name), checkEmail(b.email), checkNewPassword(b.password), checkOrganization(b.organization));
1578
1588
  if (bad) return invalid(bad);
1579
1589
  const email = String(b.email).trim();
1580
- const made = await ask(request, "POST", "/v1/auth/register", { email, password: b.password, display_name: typeof b.name === "string" ? b.name.trim() : "" }, pages.verifyEmail);
1590
+ const organization = typeof b.organization === "string" && b.organization.trim() !== "" ? { organization: { name: b.organization.trim() } } : {};
1591
+ 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);
1581
1592
  if (made.error) return problem(made.status, made.error.code, made.error.message);
1582
1593
  if (made.data?.verification_required === true) return json({ status: "verification_required", email });
1583
1594
  const res = await ask(request, "POST", "/v1/auth/login", { email, password: b.password });
@@ -1779,6 +1790,7 @@ function createCauthy(given = {}) {
1779
1790
  if (!isToken(b.token)) return badLink();
1780
1791
  const res = await ask(request, "POST", "/v1/auth/email/verify", { token: String(b.token) });
1781
1792
  if (res.error) return problem(res.status, res.error.code, res.error.message);
1793
+ if (res.data?.access_token) return signedIn(request, res.data);
1782
1794
  return json({ status: "verified" });
1783
1795
  }
1784
1796
  if (route === "/sign-in/passkey/begin") {