cauthy-auth 0.22.0 → 0.23.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/react.mjs CHANGED
@@ -272,6 +272,35 @@ function createAuthClient(options = {}) {
272
272
  mfa: (f) => post("/sign-in/mfa", { code: f.code }, checkCode(f.code)),
273
273
  mfaMethods: () => post("/sign-in/mfa/methods", {}, null),
274
274
  mfaSendCode: () => post("/sign-in/mfa/send", {}, null),
275
+ mfaPush: () => post("/sign-in/mfa/push", {}, null),
276
+ async mfaPushWait(o = {}) {
277
+ const status = await new Promise((resolve) => {
278
+ if (typeof EventSource !== "function") return resolve("approved");
279
+ const source = new EventSource(`${base}/sign-in/mfa/push/events`, { withCredentials: true });
280
+ const stop = (st) => {
281
+ source.close();
282
+ resolve(st);
283
+ };
284
+ o.signal?.addEventListener("abort", () => stop("aborted"));
285
+ source.addEventListener("status", (e) => {
286
+ const st = (() => {
287
+ try {
288
+ return String(JSON.parse(e.data).status);
289
+ } catch {
290
+ return "";
291
+ }
292
+ })();
293
+ if (st && st !== "pending") stop(st);
294
+ });
295
+ source.onerror = () => {
296
+ if (source.readyState === EventSource.CLOSED) stop("expired");
297
+ };
298
+ });
299
+ if (status === "aborted") return { ok: false, error: { code: "aborted", message: "Stopped waiting." } };
300
+ if (status === "denied") return { ok: false, error: { code: "push_denied", message: "The sign-in was turned down on the phone." } };
301
+ if (status === "expired") return { ok: false, error: { code: "push_expired", message: "Nobody answered on the phone in time." } };
302
+ return post("/sign-in/mfa/push/finish", {}, null);
303
+ },
275
304
  magicLink: (f) => post("/sign-in/magic-link", { email: f.email, ...f.create ? { create: true } : {}, ...f.returnTo ? { returnTo: f.returnTo } : {} }, checkEmail(f.email), f.captchaToken),
276
305
  magicLinkVerify: (f) => post("/sign-in/magic-link/verify", { token: f.token }, null),
277
306
  emailCode: (f) => post("/sign-in/email-code", { code: f.code }, checkCode(f.code)),
@@ -536,6 +565,9 @@ var TEXTS = {
536
565
  sendCodeSms: "Send a code by SMS to {to}",
537
566
  sendCodeWhatsapp: "Send a code by WhatsApp to {to}",
538
567
  codeSent: "We sent a code to {to}. Type it here.",
568
+ pushSend: "Approve on my phone",
569
+ pushPick: "On your phone, pick {n}",
570
+ pushWaiting: "We sent a notification to your phone. Open it and pick this number.",
539
571
  phone: "Phone, with its country code",
540
572
  phoneHelp: "Get your codes by SMS or WhatsApp instead of an app.",
541
573
  phoneOn: "Codes go to {to}.",
@@ -682,6 +714,9 @@ var TEXTS = {
682
714
  sendCodeSms: "Enviar un c\xF3digo por SMS a {to}",
683
715
  sendCodeWhatsapp: "Enviar un c\xF3digo por WhatsApp a {to}",
684
716
  codeSent: "Te enviamos un c\xF3digo a {to}. Escr\xEDbelo aqu\xED.",
717
+ pushSend: "Aprobar en mi tel\xE9fono",
718
+ pushPick: "En tu tel\xE9fono, elige el {n}",
719
+ pushWaiting: "Enviamos una notificaci\xF3n a tu tel\xE9fono. \xC1brela y elige este n\xFAmero.",
685
720
  phone: "Tel\xE9fono, con su c\xF3digo de pa\xEDs",
686
721
  phoneHelp: "Recibe tus c\xF3digos por SMS o WhatsApp en vez de una app.",
687
722
  phoneOn: "Los c\xF3digos van a {to}.",
@@ -984,9 +1019,28 @@ function SignIn(props) {
984
1019
  useEffect(() => {
985
1020
  if (step !== "code") return;
986
1021
  void auth.signIn.mfaMethods().then((r) => {
987
- if (r.ok) setPhoneWay({ totp: r.totp, phone: r.phone });
1022
+ if (r.ok) setPhoneWay({ totp: r.totp, phone: r.phone, push: r.push });
988
1023
  });
989
1024
  }, [step]);
1025
+ const [pushNumber, setPushNumber] = useState(0);
1026
+ const pushWait = useRef(null);
1027
+ useEffect(() => () => pushWait.current?.abort(), []);
1028
+ const sendPush = async () => {
1029
+ f.setBusy(true);
1030
+ f.setError(null);
1031
+ const r = await auth.signIn.mfaPush();
1032
+ f.setBusy(false);
1033
+ if (!r.ok) return f.setError(r.error);
1034
+ setPushNumber(r.number);
1035
+ pushWait.current?.abort();
1036
+ const ctl = new AbortController();
1037
+ pushWait.current = ctl;
1038
+ const done = await auth.signIn.mfaPushWait({ signal: ctl.signal });
1039
+ if (ctl.signal.aborted) return;
1040
+ setPushNumber(0);
1041
+ if (!done.ok) return f.setError(done.error);
1042
+ f.done();
1043
+ };
990
1044
  const sendCode = async () => {
991
1045
  f.setBusy(true);
992
1046
  f.setError(null);
@@ -1098,6 +1152,11 @@ function SignIn(props) {
1098
1152
  return /* @__PURE__ */ jsxs("form", { className: f.cx("form"), ...f.root, onSubmit: submit, noValidate: true, children: [
1099
1153
  /* @__PURE__ */ jsx("p", { className: f.cx("notice"), children: phoneSent ? f.t.codeSent.replace("{to}", phoneSent) : phoneWay?.phone && !phoneWay.totp ? f.t.codeHelpPhone : f.t.codeHelp }),
1100
1154
  general && /* @__PURE__ */ jsx("p", { role: "alert", className: f.cx("error"), children: f.say(general) }),
1155
+ pushNumber > 0 ? /* @__PURE__ */ jsxs("div", { className: f.cx("notice"), role: "status", "aria-live": "polite", children: [
1156
+ /* @__PURE__ */ jsx("p", { children: f.t.pushPick.replace("{n}", String(pushNumber)) }),
1157
+ /* @__PURE__ */ jsx("p", { style: { fontSize: 48, fontWeight: 700, margin: "4px 0", textAlign: "center" }, children: pushNumber }),
1158
+ /* @__PURE__ */ jsx("p", { children: f.t.pushWaiting })
1159
+ ] }) : phoneWay?.push && /* @__PURE__ */ jsx(f.Button, { type: "button", disabled: f.busy, className: f.cx("button"), onClick: () => void sendPush(), children: f.t.pushSend }),
1101
1160
  phoneWay?.phone && /* @__PURE__ */ jsx(f.Button, { type: "button", disabled: f.busy, className: f.cx("providerButton"), onClick: () => void sendCode(), children: (phoneWay.phone.channel === "whatsapp" ? f.t.sendCodeWhatsapp : f.t.sendCodeSms).replace("{to}", to) }),
1102
1161
  /* @__PURE__ */ jsx(Field, { f, name: "code", type: "text", label: f.t.code, value: code, autoComplete: "one-time-code", required: true, onChange: setCode }),
1103
1162
  /* @__PURE__ */ jsx(f.Button, { type: "submit", disabled: f.busy, className: f.cx("button"), children: f.t.verify })
@@ -1890,6 +1949,7 @@ function useAuth() {
1890
1949
  anonymously: (f) => then(client.signIn.anonymously(f)),
1891
1950
  email: (f) => then(client.signIn.email(f)),
1892
1951
  mfa: (f) => then(client.signIn.mfa(f)),
1952
+ mfaPushWait: (o) => then(client.signIn.mfaPushWait(o)),
1893
1953
  magicLinkVerify: (f) => then(client.signIn.magicLinkVerify(f)),
1894
1954
  emailCode: (f) => then(client.signIn.emailCode(f)),
1895
1955
  passkey: (o) => then(client.signIn.passkey(o)),