@connextar/house 0.23.1 → 0.24.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/README.md +55 -13
- package/dist/analytics/browser.d.ts +1 -0
- package/dist/analytics/browser.d.ts.map +1 -1
- package/dist/analytics/browser.js +22 -5
- package/dist/analytics/browser.js.map +1 -1
- package/dist/analytics/config.d.ts +5 -0
- package/dist/analytics/config.d.ts.map +1 -1
- package/dist/analytics/config.js +4 -1
- package/dist/analytics/config.js.map +1 -1
- package/dist/analytics/posthog-extensions.d.ts +19 -0
- package/dist/analytics/posthog-extensions.d.ts.map +1 -0
- package/dist/analytics/posthog-extensions.js +19 -0
- package/dist/analytics/posthog-extensions.js.map +1 -0
- package/dist/analytics/react.d.ts.map +1 -1
- package/dist/analytics/react.js +4 -2
- package/dist/analytics/react.js.map +1 -1
- package/dist/analytics/redact.d.ts.map +1 -1
- package/dist/analytics/redact.js +6 -1
- package/dist/analytics/redact.js.map +1 -1
- package/dist/house.css +19 -0
- package/package.json +17 -3
- package/src/account/data-contract.ts +112 -0
- package/src/account/index.ts +11 -0
- package/src/admin/access.ts +267 -0
- package/src/admin/audit.ts +238 -0
- package/src/admin/bootstrap.ts +220 -0
- package/src/admin/client-index.ts +19 -0
- package/src/admin/client.tsx +187 -0
- package/src/admin/index.ts +91 -0
- package/src/admin/proof.ts +155 -0
- package/src/admin/second-factor-index.ts +16 -0
- package/src/admin/second-factor.ts +403 -0
- package/src/admin/testing-index.ts +13 -0
- package/src/admin/testing.ts +299 -0
- package/src/admin/view-as.ts +511 -0
- package/src/ai/config-index.ts +40 -0
- package/src/ai/config.ts +176 -0
- package/src/ai/images.ts +18 -0
- package/src/ai/index.ts +21 -0
- package/src/ai/pricing.ts +107 -0
- package/src/ai/provider.ts +403 -0
- package/src/analytics/browser.ts +400 -0
- package/src/analytics/client-index.ts +12 -0
- package/src/analytics/config.ts +89 -0
- package/src/analytics/index.ts +17 -0
- package/src/analytics/ingest.ts +81 -0
- package/src/analytics/posthog-extensions.ts +19 -0
- package/src/analytics/react-index.ts +16 -0
- package/src/analytics/react.tsx +241 -0
- package/src/analytics/redact.ts +146 -0
- package/src/analytics/server-index.ts +17 -0
- package/src/analytics/server.ts +277 -0
- package/src/auth/index.ts +30 -0
- package/src/auth/second-factor-index.ts +16 -0
- package/src/auth/second-factor-next.ts +136 -0
- package/src/auth/second-factor-routes.ts +40 -0
- package/src/auth/second-factor.ts +226 -0
- package/src/auth/step-up-client-index.ts +13 -0
- package/src/auth/step-up-client.ts +23 -0
- package/src/auth/step-up-index.ts +19 -0
- package/src/auth/step-up-routes.ts +99 -0
- package/src/auth/step-up.ts +432 -0
- package/src/auth/two-factor-policy.ts +112 -0
- package/src/changelog/changelog.tsx +367 -0
- package/src/changelog/index.ts +12 -0
- package/src/editor/block-handle.tsx +312 -0
- package/src/editor/blocks.ts +169 -0
- package/src/editor/bubble-toolbar.tsx +208 -0
- package/src/editor/collapsible.ts +159 -0
- package/src/editor/content-index.ts +1 -0
- package/src/editor/content.tsx +343 -0
- package/src/editor/editor.tsx +333 -0
- package/src/editor/emoji-index.ts +1 -0
- package/src/editor/emoji-picker.tsx +114 -0
- package/src/editor/emoji.ts +436 -0
- package/src/editor/index.ts +6 -0
- package/src/editor/sections.ts +83 -0
- package/src/editor/suggestion-popup.tsx +136 -0
- package/src/editor/suggestions.ts +309 -0
- package/src/editor/ui.tsx +171 -0
- package/src/errors/error-shell.tsx +72 -0
- package/src/errors/global-error-shell.tsx +116 -0
- package/src/errors/index.ts +2 -0
- package/src/grid/columns.ts +48 -0
- package/src/grid/compare.ts +35 -0
- package/src/grid/hidden-columns.ts +95 -0
- package/src/grid/index.ts +27 -0
- package/src/grid/use-grid.ts +282 -0
- package/src/grid/views.ts +70 -0
- package/src/guides/guide-article.tsx +92 -0
- package/src/guides/guide-figure-zoom.tsx +533 -0
- package/src/guides/guide-figure.tsx +68 -0
- package/src/guides/guide-toc.tsx +133 -0
- package/src/guides/guides-landing.tsx +57 -0
- package/src/guides/guides-search.tsx +160 -0
- package/src/guides/guides-shell.tsx +60 -0
- package/src/guides/guides-sidebar.tsx +93 -0
- package/src/guides/index.ts +28 -0
- package/src/guides/library.ts +165 -0
- package/src/health/health.ts +115 -0
- package/src/health/index.ts +11 -0
- package/src/internal/cx.ts +4 -0
- package/src/internal/next-link.ts +23 -0
- package/src/jobs/counts.ts +151 -0
- package/src/jobs/cron-auth.ts +60 -0
- package/src/jobs/health.ts +315 -0
- package/src/jobs/idle.ts +86 -0
- package/src/jobs/index.ts +120 -0
- package/src/jobs/job-health-list.tsx +316 -0
- package/src/jobs/next-index.ts +4 -0
- package/src/jobs/next.ts +65 -0
- package/src/jobs/prisma-index.ts +12 -0
- package/src/jobs/prisma.ts +170 -0
- package/src/jobs/prune.ts +79 -0
- package/src/jobs/react-index.ts +35 -0
- package/src/jobs/registry.ts +189 -0
- package/src/jobs/scheduler.ts +437 -0
- package/src/jobs/store.ts +212 -0
- package/src/jobs/windows.ts +157 -0
- package/src/maintenance/core.ts +270 -0
- package/src/maintenance/gate.ts +171 -0
- package/src/maintenance/index.ts +40 -0
- package/src/maintenance/page.ts +76 -0
- package/src/maintenance/proxy.ts +88 -0
- package/src/maintenance/react-index.ts +7 -0
- package/src/maintenance/react.tsx +190 -0
- package/src/maintenance/route.ts +82 -0
- package/src/maintenance/unavailable.ts +61 -0
- package/src/markdown/figure.tsx +132 -0
- package/src/markdown/figures.ts +237 -0
- package/src/markdown/guide.tsx +536 -0
- package/src/markdown/index.ts +53 -0
- package/src/markdown/table-view.tsx +74 -0
- package/src/markdown/table.ts +90 -0
- package/src/password/common.ts +58 -0
- package/src/password/estimate-index.ts +18 -0
- package/src/password/estimate.ts +715 -0
- package/src/password/index.ts +40 -0
- package/src/password/password-input.tsx +116 -0
- package/src/password/password-strength.tsx +157 -0
- package/src/password/reveal.ts +151 -0
- package/src/rate-limit/better-auth.ts +94 -0
- package/src/rate-limit/index.ts +32 -0
- package/src/rate-limit/keys.ts +110 -0
- package/src/rate-limit/limiter.ts +276 -0
- package/src/rate-limit/prisma-index.ts +12 -0
- package/src/rate-limit/prisma.ts +157 -0
- package/src/rate-limit/store.ts +47 -0
- package/src/release/changelog.ts +364 -0
- package/src/release/index.ts +32 -0
- package/src/release/version.ts +64 -0
- package/src/setup-guide/bits.tsx +420 -0
- package/src/setup-guide/card.tsx +358 -0
- package/src/setup-guide/class-names.ts +144 -0
- package/src/setup-guide/context.tsx +239 -0
- package/src/setup-guide/guide.ts +271 -0
- package/src/setup-guide/index.ts +76 -0
- package/src/setup-guide/launcher.tsx +207 -0
- package/src/setup-guide/strip.tsx +127 -0
- package/src/styles/house.css +403 -0
- package/src/trail/index.ts +12 -0
- package/src/trail/rules.ts +84 -0
- package/src/trail/trail.tsx +367 -0
- package/src/ux/errors.ts +32 -0
- package/src/ux/feedback.tsx +41 -0
- package/src/ux/index.ts +44 -0
- package/src/ux/navigation-index.ts +16 -0
- package/src/ux/navigation-progress.tsx +270 -0
- package/src/ux/optimistic.ts +415 -0
- package/src/ux/use-action.ts +155 -0
- package/src/ux/use-optimistic-list.ts +227 -0
- package/src/ux/use-optimistic-value.ts +136 -0
- package/src/web-search/index.ts +13 -0
- package/src/web-search/providers.ts +177 -0
- package/src/web-search/types.ts +98 -0
- package/src/whats-new/body.tsx +190 -0
- package/src/whats-new/index.ts +43 -0
- package/src/whats-new/seen.ts +40 -0
- package/src/whats-new/select.ts +230 -0
- package/src/whats-new/use-whats-new.ts +109 -0
- package/src/wizard/index.ts +61 -0
- package/src/wizard/invalid.ts +152 -0
- package/src/wizard/react-hook-form-index.ts +11 -0
- package/src/wizard/rhf-adapter.ts +67 -0
- package/src/wizard/rules.ts +137 -0
- package/src/wizard/wizard.tsx +442 -0
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export {
|
|
2
|
+
TWO_FACTOR_BLOCKED_MESSAGE,
|
|
3
|
+
TWO_FACTOR_BLOCKED_PATHS,
|
|
4
|
+
isTwoFactorBlockedPath,
|
|
5
|
+
magicLinkRefusalRetired,
|
|
6
|
+
shouldBlockForTwoFactor,
|
|
7
|
+
totpSecretFromUri,
|
|
8
|
+
type AuthContextLike,
|
|
9
|
+
type TwoFactorBlockedPath,
|
|
10
|
+
type TwoFactorLookup,
|
|
11
|
+
} from "./two-factor-policy.js";
|
|
12
|
+
export {
|
|
13
|
+
DEFAULT_CODE_SCREEN_PATH,
|
|
14
|
+
DEFAULT_NEXT_PARAM,
|
|
15
|
+
SECOND_FACTOR_CHALLENGE_ROUTES,
|
|
16
|
+
SECOND_FACTOR_EVERYWHERE_ID,
|
|
17
|
+
type SecondFactorChallengeRoute,
|
|
18
|
+
} from "./second-factor-routes.js";
|
|
19
|
+
export { magicLinkConfirmHref, magicLinkVerifyHref, safeNextPath, secondFactorNext } from "./second-factor-next.js";
|
|
20
|
+
export {
|
|
21
|
+
DEFAULT_STEP_UP_WINDOW_SECONDS,
|
|
22
|
+
STEP_UP_ERROR_CODES,
|
|
23
|
+
STEP_UP_ID,
|
|
24
|
+
STEP_UP_PASSKEY_ROUTES,
|
|
25
|
+
isStepUpRequired,
|
|
26
|
+
stepUpNeedsOf,
|
|
27
|
+
type StepUpErrorCode,
|
|
28
|
+
type StepUpNeeds,
|
|
29
|
+
type StepUpStatus,
|
|
30
|
+
} from "./step-up-routes.js";
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@connextar/house/auth/second-factor` — the server half: a Better Auth plugin. Kept off `@connextar/house/auth`
|
|
3
|
+
* so that entry point stays free of any Better Auth import and safe for client components.
|
|
4
|
+
*/
|
|
5
|
+
export {
|
|
6
|
+
findTwoFactorSignInHook,
|
|
7
|
+
secondFactorEverywhere,
|
|
8
|
+
type SecondFactorEverywhereOptions,
|
|
9
|
+
} from "./second-factor.js";
|
|
10
|
+
export {
|
|
11
|
+
DEFAULT_CODE_SCREEN_PATH,
|
|
12
|
+
DEFAULT_NEXT_PARAM,
|
|
13
|
+
SECOND_FACTOR_CHALLENGE_ROUTES,
|
|
14
|
+
SECOND_FACTOR_EVERYWHERE_ID,
|
|
15
|
+
type SecondFactorChallengeRoute,
|
|
16
|
+
} from "./second-factor-routes.js";
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { DEFAULT_NEXT_PARAM } from "./second-factor-routes.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Where to send someone once their second factor checks out — and, as importantly, where not to.
|
|
5
|
+
*
|
|
6
|
+
* The code screen is reached by a redirect that carries the page the person was originally heading for as `?next=`.
|
|
7
|
+
* Anything in a URL is attacker-controlled, so `next` is only ever honoured as a path on this site: never another
|
|
8
|
+
* origin, never a protocol-relative `//host`, never a backslash (browsers read `/\host` as `//host`), never a
|
|
9
|
+
* `javascript:` URL, never control characters. Anything else falls back.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const PROBE_ORIGIN = "http://next.invalid";
|
|
13
|
+
const MAX_NEXT_LENGTH = 2048;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* `candidate` if it is a same-site path, otherwise `fallback`. The result always starts with a single `/`, so it is
|
|
17
|
+
* safe to hand straight to `router.replace()` or `redirect()`.
|
|
18
|
+
*/
|
|
19
|
+
export function safeNextPath(candidate: string | null | undefined, fallback = "/"): string {
|
|
20
|
+
if (typeof candidate !== "string" || candidate.length === 0 || candidate.length > MAX_NEXT_LENGTH) return fallback;
|
|
21
|
+
if (!candidate.startsWith("/") || candidate.startsWith("//")) return fallback;
|
|
22
|
+
// Backslashes and control characters are normalised away by browsers in ways that turn `/\evil.com` into
|
|
23
|
+
// `//evil.com`; nothing legitimate needs them in a path we generated.
|
|
24
|
+
// eslint-disable-next-line no-control-regex
|
|
25
|
+
if (/[\\\u0000-\u001f\u007f]/.test(candidate)) return fallback;
|
|
26
|
+
let url: URL;
|
|
27
|
+
try {
|
|
28
|
+
url = new URL(candidate, PROBE_ORIGIN);
|
|
29
|
+
} catch {
|
|
30
|
+
return fallback;
|
|
31
|
+
}
|
|
32
|
+
if (url.origin !== PROBE_ORIGIN) return fallback;
|
|
33
|
+
return `${url.pathname}${url.search}${url.hash}`;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The code screen's `next`, made safe. Pass the page's search params (a `URLSearchParams`, Next's
|
|
38
|
+
* `ReadonlyURLSearchParams`, or a raw `?a=b` string); read it once the code — or a backup code — has been accepted:
|
|
39
|
+
*
|
|
40
|
+
* ```ts
|
|
41
|
+
* const { error } = await authClient.twoFactor.verifyTotp({ code, trustDevice });
|
|
42
|
+
* if (!error) router.replace(secondFactorNext(useSearchParams()));
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export function secondFactorNext(
|
|
46
|
+
search: string | { get(name: string): string | null } | null | undefined,
|
|
47
|
+
options: { param?: string; fallback?: string } = {},
|
|
48
|
+
): string {
|
|
49
|
+
const param = options.param ?? DEFAULT_NEXT_PARAM;
|
|
50
|
+
const fallback = options.fallback ?? "/";
|
|
51
|
+
if (!search) return fallback;
|
|
52
|
+
const params = typeof search === "string" ? new URLSearchParams(search) : search;
|
|
53
|
+
return safeNextPath(params.get(param), fallback);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The same-site path of `location` (absolute or relative) on `origin`, or null when it points anywhere else.
|
|
58
|
+
* Used by the server to turn a sign-in route's `callbackURL` into the code screen's `next`.
|
|
59
|
+
*/
|
|
60
|
+
export function sameSitePath(location: string | null | undefined, origin: string): string | null {
|
|
61
|
+
if (!location) return null;
|
|
62
|
+
let url: URL;
|
|
63
|
+
let base: URL;
|
|
64
|
+
try {
|
|
65
|
+
base = new URL(origin);
|
|
66
|
+
url = new URL(location, base);
|
|
67
|
+
} catch {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
if (url.origin !== base.origin) return null;
|
|
71
|
+
const path = safeNextPath(`${url.pathname}${url.search}${url.hash}`, "");
|
|
72
|
+
return path === "" ? null : path;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* The code screen's URL on `origin`, carrying `next` when it is worth carrying: a safe same-site path that is not
|
|
77
|
+
* the site root (the default anyway) and not the code screen itself (which would loop).
|
|
78
|
+
*/
|
|
79
|
+
export function codeScreenUrl(
|
|
80
|
+
origin: string,
|
|
81
|
+
codeScreenPath: string,
|
|
82
|
+
next: string | null,
|
|
83
|
+
param: string = DEFAULT_NEXT_PARAM,
|
|
84
|
+
): string {
|
|
85
|
+
const url = new URL(codeScreenPath, origin);
|
|
86
|
+
if (next && next !== "/" && new URL(next, origin).pathname !== url.pathname) url.searchParams.set(param, next);
|
|
87
|
+
return url.toString();
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Email link scanners (Microsoft Defender Safe Links, Mimecast, Proofpoint…) open links before the person does.
|
|
92
|
+
* A magic link is single-use, so the scanner spends it: without two-factor the scanner is handed a session it
|
|
93
|
+
* throws away, with two-factor it is handed the pending code state — and either way the person's own click then
|
|
94
|
+
* finds the link used.
|
|
95
|
+
*
|
|
96
|
+
* The mitigation is a confirm step: email a link to a page of the app that shows a "Continue signing in" button,
|
|
97
|
+
* and only that button goes to Better Auth's verify URL. Scanners fetch; they do not press buttons.
|
|
98
|
+
*
|
|
99
|
+
* `magicLinkConfirmHref()` turns the verify URL Better Auth gives `sendMagicLink` into that page's URL, carrying
|
|
100
|
+
* only the token and the redirect targets; `magicLinkVerifyHref()` rebuilds the verify URL on the page, from the
|
|
101
|
+
* app's own fixed verify path — never from a URL taken off the query string, which would be an open redirect. The
|
|
102
|
+
* verify endpoint still origin-checks the redirect targets itself.
|
|
103
|
+
*/
|
|
104
|
+
const MAGIC_LINK_PARAMS = ["token", "callbackURL", "newUserCallbackURL", "errorCallbackURL"] as const;
|
|
105
|
+
|
|
106
|
+
export function magicLinkConfirmHref(verifyUrl: string, confirmPath: string): string {
|
|
107
|
+
const source = new URL(verifyUrl);
|
|
108
|
+
const target = new URL(confirmPath, source.origin);
|
|
109
|
+
for (const name of MAGIC_LINK_PARAMS) {
|
|
110
|
+
const value = source.searchParams.get(name);
|
|
111
|
+
if (value !== null) target.searchParams.set(name, value);
|
|
112
|
+
}
|
|
113
|
+
return target.toString();
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* The verify URL to send the "Continue" button to, or null when the page was opened without a token. `verifyPath`
|
|
118
|
+
* is the app's own route, e.g. `/api/auth/magic-link/verify`.
|
|
119
|
+
*/
|
|
120
|
+
export function magicLinkVerifyHref(
|
|
121
|
+
search: string | { get(name: string): string | null },
|
|
122
|
+
verifyPath: string,
|
|
123
|
+
): string | null {
|
|
124
|
+
const params = typeof search === "string" ? new URLSearchParams(search) : search;
|
|
125
|
+
const token = params.get("token");
|
|
126
|
+
if (!token) return null;
|
|
127
|
+
const out = new URLSearchParams();
|
|
128
|
+
for (const name of MAGIC_LINK_PARAMS) {
|
|
129
|
+
const value = name === "token" ? token : params.get(name);
|
|
130
|
+
if (value === null) continue;
|
|
131
|
+
// Redirect targets must be same-site paths here; Better Auth resolves them against its own base URL.
|
|
132
|
+
if (name !== "token" && safeNextPath(value, "") === "") continue;
|
|
133
|
+
out.set(name, value);
|
|
134
|
+
}
|
|
135
|
+
return `${safeNextPath(verifyPath)}?${out.toString()}`;
|
|
136
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The vocabulary shared by the server plugin (`@connextar/house/auth/second-factor`) and the pure helpers in
|
|
3
|
+
* `@connextar/house/auth`. Kept free of any Better Auth import so a client component or a before-hook can read it
|
|
4
|
+
* without pulling the server in.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/** The Better Auth plugin id of `secondFactorEverywhere()`. */
|
|
8
|
+
export const SECOND_FACTOR_EVERYWHERE_ID = "second-factor-everywhere";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Every route that hands out a session without a password, and so without Better Auth's two-factor check — the
|
|
12
|
+
* ones `secondFactorEverywhere()` can put the code screen behind.
|
|
13
|
+
*
|
|
14
|
+
* - `/magic-link/verify` — the link in the email.
|
|
15
|
+
* - `/callback/:id` — Google, Microsoft, Apple and every other built-in social provider's OAuth callback.
|
|
16
|
+
* - `/oauth2/callback/:providerId` — the generic-OAuth plugin's callback.
|
|
17
|
+
* - `/sign-in/social` — only when called with an `idToken` (native Google/Apple sign-in); the redirect flow makes no
|
|
18
|
+
* session here, so there is nothing to challenge.
|
|
19
|
+
* - `/one-tap/callback` — Google One Tap.
|
|
20
|
+
* - `/sign-in/email-otp` — the email-OTP plugin's sign-in.
|
|
21
|
+
*
|
|
22
|
+
* Passkeys (`/passkey/verify-authentication`) are deliberately absent and cannot be added: a passkey is possession
|
|
23
|
+
* of a device and phishing-resistant, at least as strong as password + code, so it stays a single step.
|
|
24
|
+
*/
|
|
25
|
+
export const SECOND_FACTOR_CHALLENGE_ROUTES = [
|
|
26
|
+
"/magic-link/verify",
|
|
27
|
+
"/callback/:id",
|
|
28
|
+
"/oauth2/callback/:providerId",
|
|
29
|
+
"/sign-in/social",
|
|
30
|
+
"/one-tap/callback",
|
|
31
|
+
"/sign-in/email-otp",
|
|
32
|
+
] as const;
|
|
33
|
+
|
|
34
|
+
export type SecondFactorChallengeRoute = (typeof SECOND_FACTOR_CHALLENGE_ROUTES)[number];
|
|
35
|
+
|
|
36
|
+
/** Where the code screen lives unless an app says otherwise — the path every house app already uses. */
|
|
37
|
+
export const DEFAULT_CODE_SCREEN_PATH = "/auth/two-factor";
|
|
38
|
+
|
|
39
|
+
/** The query parameter that carries "where to go once the code checks out". */
|
|
40
|
+
export const DEFAULT_NEXT_PARAM = "next";
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import type { BetterAuthPlugin } from "better-auth";
|
|
2
|
+
import { APIError, isAPIError } from "better-auth/api";
|
|
3
|
+
|
|
4
|
+
import { codeScreenUrl, sameSitePath } from "./second-factor-next.js";
|
|
5
|
+
import {
|
|
6
|
+
DEFAULT_CODE_SCREEN_PATH,
|
|
7
|
+
DEFAULT_NEXT_PARAM,
|
|
8
|
+
SECOND_FACTOR_CHALLENGE_ROUTES,
|
|
9
|
+
SECOND_FACTOR_EVERYWHERE_ID,
|
|
10
|
+
type SecondFactorChallengeRoute,
|
|
11
|
+
} from "./second-factor-routes.js";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The second factor after a magic link and after a social sign-in — every sign-in path consistent.
|
|
15
|
+
*
|
|
16
|
+
* Better Auth's `twoFactor` plugin swaps the session for a pending "enter your code" state only on the password
|
|
17
|
+
* routes (`/sign-in/email`, `/sign-in/username`, `/sign-in/phone-number`). A magic link (`/magic-link/verify`) and a
|
|
18
|
+
* social callback (`/callback/:id`) hand out a full session with no code. This plugin runs **the two-factor plugin's
|
|
19
|
+
* own after-hook** on those routes too, so an account with two-factor on gets exactly what a password sign-in gets:
|
|
20
|
+
*
|
|
21
|
+
* - no session survives — the one the route just made is deleted and its cookies expired;
|
|
22
|
+
* - the pending state is Better Auth's own (its signed `two_factor` cookie and verification row), so
|
|
23
|
+
* `/two-factor/verify-totp`, `/two-factor/verify-backup-code` and `/two-factor/verify-otp` finish the sign-in
|
|
24
|
+
* unchanged — trust-this-device, attempt limits, account lockout and all;
|
|
25
|
+
* - a device with a valid trust cookie skips the code, by the plugin's own rule, and has the cookie rotated;
|
|
26
|
+
* - a browser redirect (the link, the OAuth callback) is sent to the app's code screen instead of `callbackURL`, with
|
|
27
|
+
* the original destination carried as a same-site `?next=` — anything off-site is dropped;
|
|
28
|
+
* - a JSON caller (magic-link verify without a `callbackURL`, `/sign-in/social` with an `idToken`) gets the plugin's
|
|
29
|
+
* own `{ twoFactorRedirect: true }`, which `twoFactorClient`'s `onTwoFactorRedirect` already handles.
|
|
30
|
+
*
|
|
31
|
+
* Nothing about Better Auth's pending state is copied here — no cookie names, no signing, no verification
|
|
32
|
+
* identifiers — so a Better Auth upgrade that changes them changes them here too. What this relies on is that the
|
|
33
|
+
* two-factor plugin exposes its after-hook, that the hook reads `ctx.context.newSession`, and that it answers
|
|
34
|
+
* `{ twoFactorRedirect: true }` when it takes the session away; `second-factor.test.ts` pins each of those against
|
|
35
|
+
* the installed Better Auth, and `init` refuses to start if the hook cannot be found.
|
|
36
|
+
*
|
|
37
|
+
* Passkeys are untouched: a passkey is possession of a device and phishing-resistant, so it stays one step.
|
|
38
|
+
*
|
|
39
|
+
* **Order**: list it after `twoFactor()` and before `nextCookies()`. It composes with an app's own
|
|
40
|
+
* `second-factor-session` plugin unchanged: the session a magic link or social callback creates is deleted before the
|
|
41
|
+
* response leaves, and the session that counts is created by `/two-factor/verify-totp` (or the backup-code route),
|
|
42
|
+
* which those plugins already record as second-factor-proven.
|
|
43
|
+
*/
|
|
44
|
+
export interface SecondFactorEverywhereOptions {
|
|
45
|
+
/**
|
|
46
|
+
* Turn the challenge off for this app (`false`) while keeping the plugin listed. With it off, `magicLinkRefusalRetired()`
|
|
47
|
+
* answers false again, so an app still using the old magic-link refusal gets it back. Default `true`.
|
|
48
|
+
*/
|
|
49
|
+
enabled?: boolean;
|
|
50
|
+
/** The app's code screen, a path on the app's origin. Default `/auth/two-factor`. */
|
|
51
|
+
codeScreenPath?: string;
|
|
52
|
+
/** The query parameter that carries the original destination to the code screen. Default `next`. */
|
|
53
|
+
nextParam?: string;
|
|
54
|
+
/**
|
|
55
|
+
* The origin the code screen (and every acceptable `next`) lives on. Default: the origin of Better Auth's
|
|
56
|
+
* `baseURL`, which is the app itself whenever auth is mounted under the app (`/api/auth`).
|
|
57
|
+
*/
|
|
58
|
+
appOrigin?: string;
|
|
59
|
+
/** Which routes get the challenge. Default: all of {@link SECOND_FACTOR_CHALLENGE_ROUTES}. */
|
|
60
|
+
routes?: readonly SecondFactorChallengeRoute[];
|
|
61
|
+
/**
|
|
62
|
+
* Which OAuth providers get the challenge on the social routes (`/callback/:id`, `/oauth2/callback/:providerId`,
|
|
63
|
+
* `/sign-in/social`, `/one-tap/callback` counts as `google`). Default `"all"`.
|
|
64
|
+
*/
|
|
65
|
+
providers?: "all" | readonly string[];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
type Hooks = NonNullable<BetterAuthPlugin["hooks"]>;
|
|
69
|
+
type AfterHook = NonNullable<Hooks["after"]>[number];
|
|
70
|
+
|
|
71
|
+
/** The slice of the endpoint context an after-hook is handed that this reads. */
|
|
72
|
+
interface AfterHookInput {
|
|
73
|
+
path?: string;
|
|
74
|
+
params?: Record<string, string | undefined> | null;
|
|
75
|
+
body?: unknown;
|
|
76
|
+
context: {
|
|
77
|
+
baseURL: string;
|
|
78
|
+
returned?: unknown;
|
|
79
|
+
responseHeaders?: Headers | null;
|
|
80
|
+
newSession?: { user?: { twoFactorEnabled?: boolean | null } | null } | null;
|
|
81
|
+
options: { plugins?: readonly BetterAuthPlugin[] | null };
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
interface HookResult {
|
|
86
|
+
response?: unknown;
|
|
87
|
+
headers?: Headers | null;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
type RawHook = {
|
|
91
|
+
matcher: (context: AfterHookInput) => boolean;
|
|
92
|
+
handler: (context: AfterHookInput) => Promise<HookResult | undefined>;
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* The two-factor plugin's own after-hook: the one that matches a password sign-in. Exported for the pinning tests.
|
|
97
|
+
* Returns null when the plugin is missing, or no longer exposes a hook this way.
|
|
98
|
+
*/
|
|
99
|
+
export function findTwoFactorSignInHook(plugins: readonly BetterAuthPlugin[] | null | undefined): RawHook | null {
|
|
100
|
+
const plugin = plugins?.find((p) => p.id === "two-factor");
|
|
101
|
+
const hooks = (plugin?.hooks?.after ?? []) as unknown as RawHook[];
|
|
102
|
+
const probe = { path: "/sign-in/email" } as AfterHookInput;
|
|
103
|
+
for (const hook of hooks) {
|
|
104
|
+
try {
|
|
105
|
+
if (typeof hook.handler === "function" && hook.matcher(probe)) return hook;
|
|
106
|
+
} catch {
|
|
107
|
+
// A matcher that needs more than a path is not the one we are looking for.
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** The provider a social route is completing, or null on routes that have none. */
|
|
114
|
+
function providerOf(input: AfterHookInput): string | null {
|
|
115
|
+
switch (input.path) {
|
|
116
|
+
case "/callback/:id":
|
|
117
|
+
return input.params?.id ?? null;
|
|
118
|
+
case "/oauth2/callback/:providerId":
|
|
119
|
+
return input.params?.providerId ?? null;
|
|
120
|
+
case "/sign-in/social": {
|
|
121
|
+
const provider = (input.body as { provider?: unknown } | undefined)?.provider;
|
|
122
|
+
return typeof provider === "string" ? provider : null;
|
|
123
|
+
}
|
|
124
|
+
case "/one-tap/callback":
|
|
125
|
+
return "google";
|
|
126
|
+
default:
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function jsonBody(response: unknown): unknown {
|
|
132
|
+
// `ctx.json()` answers the plain object inside hooks, and a `{ _flag: "json", body }` wrapper when the caller
|
|
133
|
+
// asked for a Response; accept either.
|
|
134
|
+
if (response && typeof response === "object" && (response as { _flag?: unknown })._flag === "json") {
|
|
135
|
+
return (response as { body?: unknown }).body;
|
|
136
|
+
}
|
|
137
|
+
return response;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function isTwoFactorRedirect(response: unknown): boolean {
|
|
141
|
+
const body = jsonBody(response);
|
|
142
|
+
return !!body && typeof body === "object" && (body as { twoFactorRedirect?: unknown }).twoFactorRedirect === true;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function redirectLocation(input: AfterHookInput): string | null {
|
|
146
|
+
const returned = input.context.returned;
|
|
147
|
+
if (!isAPIError(returned)) return null;
|
|
148
|
+
const status = (returned as { statusCode?: number }).statusCode;
|
|
149
|
+
if (status === undefined || status < 300 || status >= 400) return null;
|
|
150
|
+
return (
|
|
151
|
+
input.context.responseHeaders?.get("location") ??
|
|
152
|
+
(returned as { headers?: Headers | null }).headers?.get?.("location") ??
|
|
153
|
+
null
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export function secondFactorEverywhere(options: SecondFactorEverywhereOptions = {}) {
|
|
158
|
+
const enabled = options.enabled !== false;
|
|
159
|
+
const routes = new Set<string>(options.routes ?? SECOND_FACTOR_CHALLENGE_ROUTES);
|
|
160
|
+
const providers = options.providers ?? "all";
|
|
161
|
+
const codeScreenPath = options.codeScreenPath ?? DEFAULT_CODE_SCREEN_PATH;
|
|
162
|
+
const nextParam = options.nextParam ?? DEFAULT_NEXT_PARAM;
|
|
163
|
+
|
|
164
|
+
if (!codeScreenPath.startsWith("/") || codeScreenPath.startsWith("//")) {
|
|
165
|
+
throw new Error(`[second-factor-everywhere] codeScreenPath must be a path on the app, got "${codeScreenPath}".`);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function applies(input: AfterHookInput): boolean {
|
|
169
|
+
if (!enabled || !input.path || !routes.has(input.path)) return false;
|
|
170
|
+
const provider = providerOf(input);
|
|
171
|
+
if (provider === null || providers === "all") return true;
|
|
172
|
+
return providers.includes(provider);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
async function handler(input: AfterHookInput): Promise<HookResult> {
|
|
176
|
+
// Only a sign-in that actually made a session for a two-factor account is ours. Account linking, errors, the
|
|
177
|
+
// redirect leg of `/sign-in/social` and accounts without two-factor all fall through untouched.
|
|
178
|
+
if (!input.context.newSession?.user?.twoFactorEnabled) return {};
|
|
179
|
+
|
|
180
|
+
const twoFactorHook = findTwoFactorSignInHook(input.context.options.plugins);
|
|
181
|
+
// `init` already refused to start without it; if it has gone missing since, fail closed rather than let the
|
|
182
|
+
// session through — the response is lost, and with it the session cookie.
|
|
183
|
+
if (!twoFactorHook) throw new Error("[second-factor-everywhere] Better Auth's two-factor plugin hook is missing.");
|
|
184
|
+
|
|
185
|
+
const location = redirectLocation(input);
|
|
186
|
+
const result = (await twoFactorHook.handler(input)) ?? {};
|
|
187
|
+
|
|
188
|
+
// Trusted device: the plugin let the session stand and rotated the trust cookie. Keep both, change nothing else.
|
|
189
|
+
if (result.response === undefined || !isTwoFactorRedirect(result.response)) return result;
|
|
190
|
+
|
|
191
|
+
// A JSON caller gets exactly what a password sign-in gets.
|
|
192
|
+
if (location === null) return result;
|
|
193
|
+
|
|
194
|
+
const origin = options.appOrigin ?? new URL(input.context.baseURL).origin;
|
|
195
|
+
const target = codeScreenUrl(origin, codeScreenPath, sameSitePath(location, origin), nextParam);
|
|
196
|
+
const headers = new Headers(result.headers ?? undefined);
|
|
197
|
+
headers.set("location", target);
|
|
198
|
+
return { response: new APIError("FOUND", undefined, { location: target }), headers };
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return {
|
|
202
|
+
id: SECOND_FACTOR_EVERYWHERE_ID,
|
|
203
|
+
options: { ...options, enabled, codeScreenPath, nextParam },
|
|
204
|
+
init(ctx) {
|
|
205
|
+
if (!enabled) return;
|
|
206
|
+
const plugins = ctx.options.plugins ?? [];
|
|
207
|
+
if (!findTwoFactorSignInHook(plugins)) {
|
|
208
|
+
throw new Error(
|
|
209
|
+
"[second-factor-everywhere] needs Better Auth's twoFactor() plugin, listed in `plugins`, exposing its " +
|
|
210
|
+
"password sign-in after-hook. Either it is missing, or this Better Auth version changed how it enforces the " +
|
|
211
|
+
"second factor — check @connextar/house's supported Better Auth range.",
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
const ownIndex = plugins.findIndex((p) => p.id === SECOND_FACTOR_EVERYWHERE_ID);
|
|
215
|
+
const nextCookiesIndex = plugins.findIndex((p) => p.id === "next-cookies");
|
|
216
|
+
if (nextCookiesIndex !== -1 && nextCookiesIndex < ownIndex) {
|
|
217
|
+
ctx.logger.warn(
|
|
218
|
+
"[second-factor-everywhere] is listed after nextCookies(); list it before, so the cookies it sets reach Next.",
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
},
|
|
222
|
+
hooks: {
|
|
223
|
+
after: [{ matcher: applies, handler } as unknown as AfterHook],
|
|
224
|
+
},
|
|
225
|
+
} satisfies BetterAuthPlugin;
|
|
226
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@connextar/house/auth/step-up/client` — the client half: a Better Auth client plugin, and the helpers a dialog
|
|
3
|
+
* needs to recognise the refusal. Nothing here imports Better Auth at runtime.
|
|
4
|
+
*/
|
|
5
|
+
export { stepUpClient } from "./step-up-client.js";
|
|
6
|
+
export {
|
|
7
|
+
STEP_UP_ERROR_CODES,
|
|
8
|
+
isStepUpRequired,
|
|
9
|
+
stepUpNeedsOf,
|
|
10
|
+
type StepUpErrorCode,
|
|
11
|
+
type StepUpNeeds,
|
|
12
|
+
type StepUpStatus,
|
|
13
|
+
} from "./step-up-routes.js";
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { BetterAuthClientPlugin } from "better-auth/client";
|
|
2
|
+
|
|
3
|
+
import type { stepUp } from "./step-up.js";
|
|
4
|
+
import { STEP_UP_ERROR_CODES, STEP_UP_ID } from "./step-up-routes.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The client half of `stepUp()`: `authClient.stepUp.status()` and `authClient.stepUp.password({ password })`, typed
|
|
8
|
+
* from the server plugin. A code confirms through Better Auth's own `authClient.twoFactor.verifyTotp({ code })` (or
|
|
9
|
+
* `verifyBackupCode`), called while signed in; nothing here wraps it.
|
|
10
|
+
*
|
|
11
|
+
* Imports nothing from Better Auth at runtime, so it costs a client bundle only its own few lines.
|
|
12
|
+
*/
|
|
13
|
+
export function stepUpClient() {
|
|
14
|
+
return {
|
|
15
|
+
id: STEP_UP_ID,
|
|
16
|
+
$InferServerPlugin: {} as ReturnType<typeof stepUp>,
|
|
17
|
+
pathMethods: {
|
|
18
|
+
"/step-up/status": "GET",
|
|
19
|
+
"/step-up/password": "POST",
|
|
20
|
+
},
|
|
21
|
+
$ERROR_CODES: STEP_UP_ERROR_CODES,
|
|
22
|
+
} satisfies BetterAuthClientPlugin;
|
|
23
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@connextar/house/auth/step-up` — the server half: a Better Auth plugin. Kept off `@connextar/house/auth` so that
|
|
3
|
+
* entry point stays free of any Better Auth import and safe for client components; the client plugin is
|
|
4
|
+
* `@connextar/house/auth/step-up/client`.
|
|
5
|
+
*/
|
|
6
|
+
export { stepUp, type PasskeyAddedData, type StepUpOptions } from "./step-up.js";
|
|
7
|
+
export {
|
|
8
|
+
DEFAULT_STEP_UP_MAX_ATTEMPTS,
|
|
9
|
+
DEFAULT_STEP_UP_WINDOW_SECONDS,
|
|
10
|
+
STEP_UP_CODE_ROUTES,
|
|
11
|
+
STEP_UP_ERROR_CODES,
|
|
12
|
+
STEP_UP_ID,
|
|
13
|
+
STEP_UP_PASSKEY_ROUTES,
|
|
14
|
+
isStepUpRequired,
|
|
15
|
+
stepUpNeedsOf,
|
|
16
|
+
type StepUpErrorCode,
|
|
17
|
+
type StepUpNeeds,
|
|
18
|
+
type StepUpStatus,
|
|
19
|
+
} from "./step-up-routes.js";
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The vocabulary shared by the step-up server plugin (`@connextar/house/auth/step-up`), its client plugin
|
|
3
|
+
* (`@connextar/house/auth/step-up/client`) and the pure helpers in `@connextar/house/auth`. Kept free of any Better
|
|
4
|
+
* Auth import so a client component can recognise a refusal without pulling the server in.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/** The Better Auth plugin id of `stepUp()` and `stepUpClient()`. */
|
|
8
|
+
export const STEP_UP_ID = "step-up";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The routes that add a passkey — a new way in. `@better-auth/passkey` guards both with nothing but a session under
|
|
12
|
+
* a day old (`session.freshAge`); `stepUp()` puts a recent proof of an existing way in in front of them.
|
|
13
|
+
*/
|
|
14
|
+
export const STEP_UP_PASSKEY_ROUTES = ["/passkey/generate-register-options", "/passkey/verify-registration"] as const;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Better Auth's own code checks. Called with a live session on an account that already has two-factor on, they check
|
|
18
|
+
* the code and create nothing — which is what `stepUp()` records as a confirmation.
|
|
19
|
+
*/
|
|
20
|
+
export const STEP_UP_CODE_ROUTES = ["/two-factor/verify-totp", "/two-factor/verify-backup-code"] as const;
|
|
21
|
+
|
|
22
|
+
/** How long a confirmation lasts unless an app says otherwise: ten minutes. */
|
|
23
|
+
export const DEFAULT_STEP_UP_WINDOW_SECONDS = 600;
|
|
24
|
+
|
|
25
|
+
/** How many wrong answers one session may give before it has to sign in again. */
|
|
26
|
+
export const DEFAULT_STEP_UP_MAX_ATTEMPTS = 5;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* What confirms "it's you" for this account:
|
|
30
|
+
*
|
|
31
|
+
* - `code` — two-factor is on: the authenticator code or a backup code, entered now.
|
|
32
|
+
* - `password` — no two-factor, but a password: the password.
|
|
33
|
+
* - `recent-sign-in` — neither (email link or social sign-in only): a sign-in within the window. Nothing to type;
|
|
34
|
+
* the screen asks them to sign in again.
|
|
35
|
+
*/
|
|
36
|
+
export type StepUpNeeds = "code" | "password" | "recent-sign-in";
|
|
37
|
+
|
|
38
|
+
/** `GET /step-up/status`, and what `POST /step-up/password` answers on success. */
|
|
39
|
+
export interface StepUpStatus {
|
|
40
|
+
needs: StepUpNeeds;
|
|
41
|
+
/** Whether this session may add a passkey now. */
|
|
42
|
+
confirmed: boolean;
|
|
43
|
+
/** When the confirmation lapses (ISO 8601), or null when there is none. */
|
|
44
|
+
confirmedUntil: string | null;
|
|
45
|
+
/** Too many wrong answers on this session: it has to sign in again before it can confirm. */
|
|
46
|
+
locked: boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Whether the session is young enough for Better Auth to let it add a passkey at all (`session.freshAge`, a day
|
|
49
|
+
* by default; always true when that check is off). When false, confirming changes nothing: the passkey plugin
|
|
50
|
+
* answers SESSION_NOT_FRESH whatever this session proves. Ask them to sign in again before asking for anything.
|
|
51
|
+
* Setting a first password has no such limit, so a set-password screen can ignore it.
|
|
52
|
+
*/
|
|
53
|
+
sessionFresh: boolean;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** The error codes the plugin answers with. Shaped as Better Auth's own `$ERROR_CODES`. */
|
|
57
|
+
export const STEP_UP_ERROR_CODES = {
|
|
58
|
+
STEP_UP_REQUIRED: { code: "STEP_UP_REQUIRED", message: "Confirm it's you before adding a passkey." },
|
|
59
|
+
STEP_UP_LOCKED: {
|
|
60
|
+
code: "STEP_UP_LOCKED",
|
|
61
|
+
message: "Too many wrong attempts. Sign in again, then confirm it's you.",
|
|
62
|
+
},
|
|
63
|
+
STEP_UP_PASSWORD_NOT_ACCEPTED: {
|
|
64
|
+
code: "STEP_UP_PASSWORD_NOT_ACCEPTED",
|
|
65
|
+
message: "This account doesn't confirm with a password.",
|
|
66
|
+
},
|
|
67
|
+
} as const;
|
|
68
|
+
|
|
69
|
+
export type StepUpErrorCode = keyof typeof STEP_UP_ERROR_CODES;
|
|
70
|
+
|
|
71
|
+
/** The refusal's message on a route that is not a passkey route (one an app added to `routes`). */
|
|
72
|
+
export const STEP_UP_REQUIRED_GENERIC_MESSAGE = "Confirm it's you before making this change.";
|
|
73
|
+
|
|
74
|
+
const NEEDS: readonly string[] = ["code", "password", "recent-sign-in"] satisfies StepUpNeeds[];
|
|
75
|
+
|
|
76
|
+
/** The error body, wherever the caller's library put it: the object itself, `.error` (Better Auth's client) or `.body`. */
|
|
77
|
+
function errorBodies(error: unknown): Record<string, unknown>[] {
|
|
78
|
+
if (!error || typeof error !== "object") return [];
|
|
79
|
+
const outer = error as Record<string, unknown>;
|
|
80
|
+
return [outer, outer.error, outer.body].filter(
|
|
81
|
+
(body): body is Record<string, unknown> => !!body && typeof body === "object",
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* True when `error` is the step-up refusal: `{ code: "STEP_UP_REQUIRED" }` as Better Auth's client returns it in
|
|
87
|
+
* `{ error }`, as a thrown `APIError` carries it in `.body`, or wrapped once more in `.error`.
|
|
88
|
+
*/
|
|
89
|
+
export function isStepUpRequired(error: unknown): boolean {
|
|
90
|
+
return errorBodies(error).some((body) => body.code === STEP_UP_ERROR_CODES.STEP_UP_REQUIRED.code);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** What the refusal (or a `STEP_UP_PASSWORD_NOT_ACCEPTED`) says confirms this account, or null if it says nothing. */
|
|
94
|
+
export function stepUpNeedsOf(error: unknown): StepUpNeeds | null {
|
|
95
|
+
for (const body of errorBodies(error)) {
|
|
96
|
+
if (typeof body.needs === "string" && NEEDS.includes(body.needs)) return body.needs as StepUpNeeds;
|
|
97
|
+
}
|
|
98
|
+
return null;
|
|
99
|
+
}
|