cauthy-auth 0.16.0 → 0.17.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.d.mts CHANGED
@@ -484,10 +484,19 @@ interface AuthClient {
484
484
  sentTo: string;
485
485
  channel: string;
486
486
  }>>;
487
- /** Mails a link that signs the person in. The answer is the same whether or not the address has an account. */
487
+ /**
488
+ * Mails a link (and a code) that signs the person in. The answer is the
489
+ * same whether or not the address has an account. `create`: an address
490
+ * with no account gets one when the link or code is used, if the
491
+ * workspace accepts new accounts — one screen signs people in and up.
492
+ * `returnTo` (a path of this site) travels in the link: the page it opens
493
+ * gets it as `?return_to=`, and `emailCode` answers it.
494
+ */
488
495
  magicLink(form: {
489
496
  email: string;
490
497
  captchaToken?: string;
498
+ create?: boolean;
499
+ returnTo?: string;
491
500
  }): Promise<AuthResult<'sent'>>;
492
501
  /** Signs in with the token the mailed link opened the page with (`ml_token`). */
493
502
  magicLinkVerify(form: {
@@ -500,6 +509,7 @@ interface AuthClient {
500
509
  code: string;
501
510
  }): Promise<AuthResult<'signed_in', {
502
511
  session: PublicSession;
512
+ returnTo?: string;
503
513
  }>>;
504
514
  /**
505
515
  * Signs in with a passkey, on this page: the browser asks the person for
@@ -709,6 +719,12 @@ interface AuthFormProps {
709
719
  onSuccess?: () => void;
710
720
  /** Where to go after signing in, and where a provider returns to. Default: `/`. */
711
721
  returnTo?: string;
722
+ /**
723
+ * `<SignIn />`: a sign-in link sent to an address with no account makes
724
+ * one when it is used (if the workspace accepts new accounts), so this one
725
+ * screen signs people in and up alike. Default: off.
726
+ */
727
+ createAccounts?: boolean;
712
728
  /** Paths of the application's other screens, for the links between them. */
713
729
  links?: {
714
730
  signIn?: string;
package/dist/react.d.ts CHANGED
@@ -484,10 +484,19 @@ interface AuthClient {
484
484
  sentTo: string;
485
485
  channel: string;
486
486
  }>>;
487
- /** Mails a link that signs the person in. The answer is the same whether or not the address has an account. */
487
+ /**
488
+ * Mails a link (and a code) that signs the person in. The answer is the
489
+ * same whether or not the address has an account. `create`: an address
490
+ * with no account gets one when the link or code is used, if the
491
+ * workspace accepts new accounts — one screen signs people in and up.
492
+ * `returnTo` (a path of this site) travels in the link: the page it opens
493
+ * gets it as `?return_to=`, and `emailCode` answers it.
494
+ */
488
495
  magicLink(form: {
489
496
  email: string;
490
497
  captchaToken?: string;
498
+ create?: boolean;
499
+ returnTo?: string;
491
500
  }): Promise<AuthResult<'sent'>>;
492
501
  /** Signs in with the token the mailed link opened the page with (`ml_token`). */
493
502
  magicLinkVerify(form: {
@@ -500,6 +509,7 @@ interface AuthClient {
500
509
  code: string;
501
510
  }): Promise<AuthResult<'signed_in', {
502
511
  session: PublicSession;
512
+ returnTo?: string;
503
513
  }>>;
504
514
  /**
505
515
  * Signs in with a passkey, on this page: the browser asks the person for
@@ -709,6 +719,12 @@ interface AuthFormProps {
709
719
  onSuccess?: () => void;
710
720
  /** Where to go after signing in, and where a provider returns to. Default: `/`. */
711
721
  returnTo?: string;
722
+ /**
723
+ * `<SignIn />`: a sign-in link sent to an address with no account makes
724
+ * one when it is used (if the workspace accepts new accounts), so this one
725
+ * screen signs people in and up alike. Default: off.
726
+ */
727
+ createAccounts?: boolean;
712
728
  /** Paths of the application's other screens, for the links between them. */
713
729
  links?: {
714
730
  signIn?: string;
package/dist/react.js CHANGED
@@ -302,7 +302,7 @@ function createAuthClient(options = {}) {
302
302
  mfa: (f) => post("/sign-in/mfa", { code: f.code }, checkCode(f.code)),
303
303
  mfaMethods: () => post("/sign-in/mfa/methods", {}, null),
304
304
  mfaSendCode: () => post("/sign-in/mfa/send", {}, null),
305
- magicLink: (f) => post("/sign-in/magic-link", { email: f.email }, checkEmail(f.email), f.captchaToken),
305
+ magicLink: (f) => post("/sign-in/magic-link", { email: f.email, ...f.create ? { create: true } : {}, ...f.returnTo ? { returnTo: f.returnTo } : {} }, checkEmail(f.email), f.captchaToken),
306
306
  magicLinkVerify: (f) => post("/sign-in/magic-link/verify", { token: f.token }, null),
307
307
  emailCode: (f) => post("/sign-in/email-code", { code: f.code }, checkCode(f.code)),
308
308
  passkeyUrl,
@@ -836,6 +836,9 @@ function useMethods(auth) {
836
836
  }, []);
837
837
  return methods;
838
838
  }
839
+ function sitePath(to) {
840
+ return to && to.startsWith("/") && !to.startsWith("//") && !to.includes("\\") ? to : void 0;
841
+ }
839
842
  function useForm(props) {
840
843
  const t = { ...TEXTS[props.locale ?? "en"], ...props.texts, errors: { ...TEXTS[props.locale ?? "en"].errors, ...props.texts?.errors } };
841
844
  const cx = (slot) => props.classNames?.[slot] ?? `cauthy-${slot}`;
@@ -844,9 +847,9 @@ function useForm(props) {
844
847
  const [error, setError] = (0, import_react.useState)(null);
845
848
  const [busy, setBusy] = (0, import_react.useState)(false);
846
849
  const say = (e) => t.errors[e.code] ?? e.message;
847
- const done = () => {
850
+ const done = (to) => {
848
851
  if (props.onSuccess) props.onSuccess();
849
- else window.location.assign(props.returnTo ?? "/");
852
+ else window.location.assign(sitePath(to) ?? props.returnTo ?? "/");
850
853
  };
851
854
  const root = useRoot(props);
852
855
  return { t, cx, Button, Input, error, setError, busy, setBusy, say, done, root };
@@ -936,7 +939,7 @@ function SignIn(props) {
936
939
  if (!token) return;
937
940
  setStep("opening");
938
941
  void auth.signIn.magicLinkVerify({ token }).then((r) => {
939
- if (r.ok) return f.done();
942
+ if (r.ok) return f.done(new URLSearchParams(window.location.search).get("return_to") ?? void 0);
940
943
  f.setError(r.error);
941
944
  setStep("password");
942
945
  });
@@ -967,7 +970,7 @@ function SignIn(props) {
967
970
  return f.setError(r.error);
968
971
  }
969
972
  if (r.status === "mfa_required") return setStep("code");
970
- f.done();
973
+ f.done("returnTo" in r && typeof r.returnTo === "string" ? r.returnTo : void 0);
971
974
  };
972
975
  const usePasskey = async () => {
973
976
  autofill.current?.abort();
@@ -984,7 +987,7 @@ function SignIn(props) {
984
987
  autofill.current?.abort();
985
988
  f.setBusy(true);
986
989
  f.setError(null);
987
- const r = await auth.signIn.magicLink({ email, captchaToken: challenge.token });
990
+ const r = await auth.signIn.magicLink({ email, captchaToken: challenge.token, create: props.createAccounts, returnTo: props.returnTo });
988
991
  f.setBusy(false);
989
992
  challenge.renew();
990
993
  if (!r.ok) return f.setError(r.error);