cauthy-auth 0.15.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/index.mjs CHANGED
@@ -1083,12 +1083,12 @@ function createCauthy(given = {}) {
1083
1083
  if (r.session === null) return { response: problem(401, "unauthenticated", "Sign in first.", void 0, headers) };
1084
1084
  return { token: r.session.accessToken, kept: r.cookies.length === 0 ? (await read(request)).value : null, headers };
1085
1085
  }
1086
- async function signedIn(request, tokens, extra = []) {
1086
+ async function signedIn(request, tokens, extra = [], more = {}) {
1087
1087
  const s = await stored(tokens);
1088
1088
  if (s === null) return problem(502, "unavailable", "Sign-in is unavailable right now. Try again in a moment.");
1089
1089
  const h = new Headers();
1090
1090
  for (const c of [...await write(request, s, readCookies(request.headers.get("cookie"))), ...extra]) h.append("set-cookie", c);
1091
- return json({ status: "signed_in", session: await toPublic(toSession(s)) }, 200, h);
1091
+ return json({ status: "signed_in", session: await toPublic(toSession(s)), ...more }, 200, h);
1092
1092
  }
1093
1093
  let lookCache = null;
1094
1094
  async function ownRoute(request, url, route) {
@@ -1507,8 +1507,10 @@ function createCauthy(given = {}) {
1507
1507
  if (bad) return invalid(bad);
1508
1508
  if (pages.signIn === void 0) return problem(500, "not_configured", "cauthy-auth: pages.signIn is required for sign-in links: it is the page the link opens");
1509
1509
  const email = String(b.email).trim();
1510
- await ask(request, "POST", "/v1/auth/magic-link/request", { email }, pages.signIn);
1511
- const step = { kind: "code", token: email, returnTo: afterSignIn };
1510
+ const back = typeof b.returnTo === "string" ? localPath(b.returnTo, afterSignIn) : afterSignIn;
1511
+ const page = back === afterSignIn ? pages.signIn : `${pages.signIn}${pages.signIn.includes("?") ? "&" : "?"}return_to=${encodeURIComponent(back)}`;
1512
+ await ask(request, "POST", "/v1/auth/magic-link/request", { email, ...b.create === true ? { create_account: true } : {} }, page);
1513
+ const step = { kind: "code", token: email, returnTo: back };
1512
1514
  return json({ status: "sent" }, 200, new Headers({ "set-cookie": writeCookie(stepCookie, await seal(step, await key), attrs(request, STEP_SECONDS)) }));
1513
1515
  }
1514
1516
  if (route === "/sign-in/email-code") {
@@ -1519,7 +1521,7 @@ function createCauthy(given = {}) {
1519
1521
  if (step === null || step.kind !== "code") return problem(400, "step_expired", "Ask for a new link: this one was asked for too long ago.");
1520
1522
  const res = await ask(request, "POST", "/v1/auth/magic-link/verify-code", { email: step.token, code: String(b.code).trim() });
1521
1523
  if (res.error) return problem(res.status, res.error.code, res.error.message, "code");
1522
- return signedIn(request, res.data, [writeCookie(stepCookie, "", attrs(request, 0))]);
1524
+ return signedIn(request, res.data, [writeCookie(stepCookie, "", attrs(request, 0))], { returnTo: step.returnTo });
1523
1525
  }
1524
1526
  if (route === "/sign-in/magic-link/verify") {
1525
1527
  if (!isToken(b.token)) return badLink();