@depup/better-auth__core 1.6.23-depup.2
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/LICENSE.md +20 -0
- package/README.md +32 -0
- package/changes.json +14 -0
- package/dist/api/index.d.mts +281 -0
- package/dist/api/index.mjs +92 -0
- package/dist/async_hooks/index.d.mts +6 -0
- package/dist/async_hooks/index.mjs +20 -0
- package/dist/async_hooks/pure.index.d.mts +6 -0
- package/dist/async_hooks/pure.index.mjs +33 -0
- package/dist/context/endpoint-context.d.mts +18 -0
- package/dist/context/endpoint-context.mjs +29 -0
- package/dist/context/global.d.mts +6 -0
- package/dist/context/global.mjs +36 -0
- package/dist/context/index.d.mts +5 -0
- package/dist/context/index.mjs +5 -0
- package/dist/context/request-state.d.mts +27 -0
- package/dist/context/request-state.mjs +47 -0
- package/dist/context/transaction.d.mts +27 -0
- package/dist/context/transaction.mjs +96 -0
- package/dist/db/adapter/factory.d.mts +17 -0
- package/dist/db/adapter/factory.mjs +905 -0
- package/dist/db/adapter/get-default-field-name.d.mts +18 -0
- package/dist/db/adapter/get-default-field-name.mjs +36 -0
- package/dist/db/adapter/get-default-model-name.d.mts +12 -0
- package/dist/db/adapter/get-default-model-name.mjs +30 -0
- package/dist/db/adapter/get-field-attributes.d.mts +26 -0
- package/dist/db/adapter/get-field-attributes.mjs +37 -0
- package/dist/db/adapter/get-field-name.d.mts +18 -0
- package/dist/db/adapter/get-field-name.mjs +31 -0
- package/dist/db/adapter/get-id-field.d.mts +36 -0
- package/dist/db/adapter/get-id-field.mjs +64 -0
- package/dist/db/adapter/get-model-name.d.mts +12 -0
- package/dist/db/adapter/get-model-name.mjs +21 -0
- package/dist/db/adapter/index.d.mts +615 -0
- package/dist/db/adapter/index.mjs +24 -0
- package/dist/db/adapter/types.d.mts +105 -0
- package/dist/db/adapter/utils.d.mts +7 -0
- package/dist/db/adapter/utils.mjs +37 -0
- package/dist/db/get-tables.d.mts +6 -0
- package/dist/db/get-tables.mjs +266 -0
- package/dist/db/index.d.mts +10 -0
- package/dist/db/index.mjs +8 -0
- package/dist/db/plugin.d.mts +12 -0
- package/dist/db/schema/account.d.mts +28 -0
- package/dist/db/schema/account.mjs +17 -0
- package/dist/db/schema/rate-limit.d.mts +18 -0
- package/dist/db/schema/rate-limit.mjs +9 -0
- package/dist/db/schema/session.d.mts +23 -0
- package/dist/db/schema/session.mjs +12 -0
- package/dist/db/schema/shared.d.mts +10 -0
- package/dist/db/schema/shared.mjs +9 -0
- package/dist/db/schema/user.d.mts +22 -0
- package/dist/db/schema/user.mjs +11 -0
- package/dist/db/schema/verification.d.mts +21 -0
- package/dist/db/schema/verification.mjs +10 -0
- package/dist/db/type.d.mts +194 -0
- package/dist/env/color-depth.d.mts +4 -0
- package/dist/env/color-depth.mjs +86 -0
- package/dist/env/env-impl.d.mts +32 -0
- package/dist/env/env-impl.mjs +81 -0
- package/dist/env/index.d.mts +4 -0
- package/dist/env/index.mjs +4 -0
- package/dist/env/logger.d.mts +48 -0
- package/dist/env/logger.mjs +79 -0
- package/dist/error/codes.d.mts +69 -0
- package/dist/error/codes.mjs +55 -0
- package/dist/error/index.d.mts +26 -0
- package/dist/error/index.mjs +27 -0
- package/dist/index.d.mts +9 -0
- package/dist/index.mjs +1 -0
- package/dist/instrumentation/api.mjs +12 -0
- package/dist/instrumentation/attributes.d.mts +11 -0
- package/dist/instrumentation/attributes.mjs +10 -0
- package/dist/instrumentation/index.d.mts +3 -0
- package/dist/instrumentation/index.mjs +3 -0
- package/dist/instrumentation/noop.mjs +42 -0
- package/dist/instrumentation/pure.index.d.mts +7 -0
- package/dist/instrumentation/pure.index.mjs +7 -0
- package/dist/instrumentation/tracer.d.mts +13 -0
- package/dist/instrumentation/tracer.mjs +53 -0
- package/dist/oauth2/client-credentials-token.d.mts +56 -0
- package/dist/oauth2/client-credentials-token.mjs +64 -0
- package/dist/oauth2/create-authorization-url.d.mts +44 -0
- package/dist/oauth2/create-authorization-url.mjs +41 -0
- package/dist/oauth2/index.d.mts +8 -0
- package/dist/oauth2/index.mjs +7 -0
- package/dist/oauth2/oauth-provider.d.mts +192 -0
- package/dist/oauth2/refresh-access-token.d.mts +54 -0
- package/dist/oauth2/refresh-access-token.mjs +73 -0
- package/dist/oauth2/reject-redirects.mjs +60 -0
- package/dist/oauth2/utils.d.mts +24 -0
- package/dist/oauth2/utils.mjs +48 -0
- package/dist/oauth2/validate-authorization-code.d.mts +85 -0
- package/dist/oauth2/validate-authorization-code.mjs +86 -0
- package/dist/oauth2/verify.d.mts +65 -0
- package/dist/oauth2/verify.mjs +202 -0
- package/dist/social-providers/apple.d.mts +133 -0
- package/dist/social-providers/apple.mjs +117 -0
- package/dist/social-providers/atlassian.d.mts +70 -0
- package/dist/social-providers/atlassian.mjs +80 -0
- package/dist/social-providers/cognito.d.mts +85 -0
- package/dist/social-providers/cognito.mjs +162 -0
- package/dist/social-providers/discord.d.mts +124 -0
- package/dist/social-providers/discord.mjs +62 -0
- package/dist/social-providers/dropbox.d.mts +69 -0
- package/dist/social-providers/dropbox.mjs +72 -0
- package/dist/social-providers/facebook.d.mts +79 -0
- package/dist/social-providers/facebook.mjs +157 -0
- package/dist/social-providers/figma.d.mts +61 -0
- package/dist/social-providers/figma.mjs +83 -0
- package/dist/social-providers/github.d.mts +102 -0
- package/dist/social-providers/github.mjs +92 -0
- package/dist/social-providers/gitlab.d.mts +123 -0
- package/dist/social-providers/gitlab.mjs +79 -0
- package/dist/social-providers/google.d.mts +123 -0
- package/dist/social-providers/google.mjs +137 -0
- package/dist/social-providers/huggingface.d.mts +83 -0
- package/dist/social-providers/huggingface.mjs +73 -0
- package/dist/social-providers/index.d.mts +1834 -0
- package/dist/social-providers/index.mjs +78 -0
- package/dist/social-providers/kakao.d.mts +161 -0
- package/dist/social-providers/kakao.mjs +70 -0
- package/dist/social-providers/kick.d.mts +73 -0
- package/dist/social-providers/kick.mjs +68 -0
- package/dist/social-providers/line.d.mts +105 -0
- package/dist/social-providers/line.mjs +110 -0
- package/dist/social-providers/linear.d.mts +68 -0
- package/dist/social-providers/linear.mjs +85 -0
- package/dist/social-providers/linkedin.d.mts +67 -0
- package/dist/social-providers/linkedin.mjs +73 -0
- package/dist/social-providers/microsoft-entra-id.d.mts +174 -0
- package/dist/social-providers/microsoft-entra-id.mjs +152 -0
- package/dist/social-providers/naver.d.mts +92 -0
- package/dist/social-providers/naver.mjs +65 -0
- package/dist/social-providers/notion.d.mts +64 -0
- package/dist/social-providers/notion.mjs +72 -0
- package/dist/social-providers/paybin.d.mts +71 -0
- package/dist/social-providers/paybin.mjs +81 -0
- package/dist/social-providers/paypal.d.mts +131 -0
- package/dist/social-providers/paypal.mjs +188 -0
- package/dist/social-providers/polar.d.mts +74 -0
- package/dist/social-providers/polar.mjs +71 -0
- package/dist/social-providers/railway.d.mts +65 -0
- package/dist/social-providers/railway.mjs +74 -0
- package/dist/social-providers/reddit.d.mts +62 -0
- package/dist/social-providers/reddit.mjs +81 -0
- package/dist/social-providers/roblox.d.mts +70 -0
- package/dist/social-providers/roblox.mjs +57 -0
- package/dist/social-providers/salesforce.d.mts +79 -0
- package/dist/social-providers/salesforce.mjs +87 -0
- package/dist/social-providers/slack.d.mts +83 -0
- package/dist/social-providers/slack.mjs +66 -0
- package/dist/social-providers/spotify.d.mts +63 -0
- package/dist/social-providers/spotify.mjs +69 -0
- package/dist/social-providers/tiktok.d.mts +168 -0
- package/dist/social-providers/tiktok.mjs +60 -0
- package/dist/social-providers/twitch.d.mts +79 -0
- package/dist/social-providers/twitch.mjs +75 -0
- package/dist/social-providers/twitter.d.mts +126 -0
- package/dist/social-providers/twitter.mjs +85 -0
- package/dist/social-providers/vercel.d.mts +62 -0
- package/dist/social-providers/vercel.mjs +58 -0
- package/dist/social-providers/vk.d.mts +70 -0
- package/dist/social-providers/vk.mjs +81 -0
- package/dist/social-providers/wechat.d.mts +113 -0
- package/dist/social-providers/wechat.mjs +81 -0
- package/dist/social-providers/zoom.d.mts +163 -0
- package/dist/social-providers/zoom.mjs +69 -0
- package/dist/types/context.d.mts +319 -0
- package/dist/types/cookie.d.mts +15 -0
- package/dist/types/helper.d.mts +10 -0
- package/dist/types/index.d.mts +9 -0
- package/dist/types/init-options.d.mts +1430 -0
- package/dist/types/plugin-client.d.mts +123 -0
- package/dist/types/plugin.d.mts +124 -0
- package/dist/types/secret.d.mts +11 -0
- package/dist/utils/async.d.mts +22 -0
- package/dist/utils/async.mjs +32 -0
- package/dist/utils/db.d.mts +9 -0
- package/dist/utils/db.mjs +15 -0
- package/dist/utils/deprecate.d.mts +9 -0
- package/dist/utils/deprecate.mjs +16 -0
- package/dist/utils/error-codes.d.mts +13 -0
- package/dist/utils/error-codes.mjs +10 -0
- package/dist/utils/fetch-metadata.d.mts +4 -0
- package/dist/utils/fetch-metadata.mjs +6 -0
- package/dist/utils/host.d.mts +147 -0
- package/dist/utils/host.mjs +295 -0
- package/dist/utils/id.d.mts +4 -0
- package/dist/utils/id.mjs +7 -0
- package/dist/utils/ip.d.mts +77 -0
- package/dist/utils/ip.mjs +230 -0
- package/dist/utils/is-api-error.d.mts +6 -0
- package/dist/utils/is-api-error.mjs +8 -0
- package/dist/utils/json.d.mts +4 -0
- package/dist/utils/json.mjs +41 -0
- package/dist/utils/redirect-uri.d.mts +20 -0
- package/dist/utils/redirect-uri.mjs +48 -0
- package/dist/utils/string.d.mts +8 -0
- package/dist/utils/string.mjs +25 -0
- package/dist/utils/url.d.mts +37 -0
- package/dist/utils/url.mjs +61 -0
- package/package.json +216 -0
- package/src/api/index.ts +185 -0
- package/src/async_hooks/index.ts +40 -0
- package/src/async_hooks/pure.index.ts +46 -0
- package/src/context/endpoint-context.ts +50 -0
- package/src/context/global.ts +57 -0
- package/src/context/index.ts +23 -0
- package/src/context/request-state.ts +91 -0
- package/src/context/transaction.ts +169 -0
- package/src/db/adapter/factory.ts +1740 -0
- package/src/db/adapter/get-default-field-name.ts +59 -0
- package/src/db/adapter/get-default-model-name.ts +51 -0
- package/src/db/adapter/get-field-attributes.ts +62 -0
- package/src/db/adapter/get-field-name.ts +43 -0
- package/src/db/adapter/get-id-field.ts +150 -0
- package/src/db/adapter/get-model-name.ts +36 -0
- package/src/db/adapter/index.ts +656 -0
- package/src/db/adapter/types.ts +134 -0
- package/src/db/adapter/utils.ts +61 -0
- package/src/db/get-tables.ts +304 -0
- package/src/db/index.ts +43 -0
- package/src/db/plugin.ts +11 -0
- package/src/db/schema/account.ts +47 -0
- package/src/db/schema/rate-limit.ts +36 -0
- package/src/db/schema/session.ts +29 -0
- package/src/db/schema/shared.ts +7 -0
- package/src/db/schema/user.ts +31 -0
- package/src/db/schema/verification.ts +28 -0
- package/src/db/type.ts +360 -0
- package/src/env/color-depth.ts +172 -0
- package/src/env/env-impl.ts +123 -0
- package/src/env/index.ts +23 -0
- package/src/env/logger.ts +145 -0
- package/src/error/codes.ts +72 -0
- package/src/error/index.ts +44 -0
- package/src/index.ts +1 -0
- package/src/instrumentation/api.ts +17 -0
- package/src/instrumentation/attributes.ts +22 -0
- package/src/instrumentation/index.ts +2 -0
- package/src/instrumentation/noop.ts +74 -0
- package/src/instrumentation/pure.index.ts +31 -0
- package/src/instrumentation/tracer.ts +95 -0
- package/src/oauth2/client-credentials-token.ts +126 -0
- package/src/oauth2/create-authorization-url.ts +89 -0
- package/src/oauth2/index.ts +34 -0
- package/src/oauth2/oauth-provider.ts +222 -0
- package/src/oauth2/refresh-access-token.ts +157 -0
- package/src/oauth2/reject-redirects.ts +70 -0
- package/src/oauth2/utils.ts +70 -0
- package/src/oauth2/validate-authorization-code.ts +190 -0
- package/src/oauth2/verify.ts +408 -0
- package/src/social-providers/apple.ts +249 -0
- package/src/social-providers/atlassian.ts +133 -0
- package/src/social-providers/cognito.ts +281 -0
- package/src/social-providers/discord.ts +170 -0
- package/src/social-providers/dropbox.ts +112 -0
- package/src/social-providers/facebook.ts +288 -0
- package/src/social-providers/figma.ts +118 -0
- package/src/social-providers/github.ts +184 -0
- package/src/social-providers/gitlab.ts +155 -0
- package/src/social-providers/google.ts +263 -0
- package/src/social-providers/huggingface.ts +119 -0
- package/src/social-providers/index.ts +132 -0
- package/src/social-providers/kakao.ts +179 -0
- package/src/social-providers/kick.ts +109 -0
- package/src/social-providers/line.ts +169 -0
- package/src/social-providers/linear.ts +121 -0
- package/src/social-providers/linkedin.ts +110 -0
- package/src/social-providers/microsoft-entra-id.ts +391 -0
- package/src/social-providers/naver.ts +113 -0
- package/src/social-providers/notion.ts +108 -0
- package/src/social-providers/paybin.ts +118 -0
- package/src/social-providers/paypal.ts +371 -0
- package/src/social-providers/polar.ts +111 -0
- package/src/social-providers/railway.ts +100 -0
- package/src/social-providers/reddit.ts +126 -0
- package/src/social-providers/roblox.ts +112 -0
- package/src/social-providers/salesforce.ts +159 -0
- package/src/social-providers/slack.ts +112 -0
- package/src/social-providers/spotify.ts +94 -0
- package/src/social-providers/tiktok.ts +211 -0
- package/src/social-providers/twitch.ts +112 -0
- package/src/social-providers/twitter.ts +199 -0
- package/src/social-providers/vercel.ts +87 -0
- package/src/social-providers/vk.ts +125 -0
- package/src/social-providers/wechat.ts +220 -0
- package/src/social-providers/zoom.ts +230 -0
- package/src/types/context.ts +460 -0
- package/src/types/cookie.ts +10 -0
- package/src/types/helper.ts +27 -0
- package/src/types/index.ts +40 -0
- package/src/types/init-options.ts +1679 -0
- package/src/types/plugin-client.ts +143 -0
- package/src/types/plugin.ts +163 -0
- package/src/types/secret.ts +8 -0
- package/src/utils/async.ts +53 -0
- package/src/utils/db.ts +20 -0
- package/src/utils/deprecate.ts +21 -0
- package/src/utils/error-codes.ts +68 -0
- package/src/utils/fetch-metadata.ts +3 -0
- package/src/utils/host.ts +416 -0
- package/src/utils/id.ts +5 -0
- package/src/utils/ip.ts +395 -0
- package/src/utils/is-api-error.ts +10 -0
- package/src/utils/json.ts +56 -0
- package/src/utils/redirect-uri.ts +54 -0
- package/src/utils/string.ts +40 -0
- package/src/utils/url.ts +77 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
|
|
3
|
+
//#region src/utils/redirect-uri.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Zod schema for OAuth redirect URIs and other developer-supplied URLs that the
|
|
6
|
+
* server stores and later hands back to a browser.
|
|
7
|
+
*
|
|
8
|
+
* - Rejects dangerous schemes (`javascript:`, `data:`, `vbscript:`).
|
|
9
|
+
* - Rejects URIs with a fragment component (`#...`) per RFC 6749 §3.1.2.
|
|
10
|
+
* - Requires HTTPS, except for loopback hosts (`127.0.0.0/8`, `[::1]`,
|
|
11
|
+
* `*.localhost` per RFC 6761), where HTTP is allowed for local development.
|
|
12
|
+
* - Allows custom schemes for mobile apps (e.g. `myapp://callback`).
|
|
13
|
+
*
|
|
14
|
+
* This is the single source of truth for redirect-URI validation across the
|
|
15
|
+
* OAuth provider plugins. Consume it from `@better-auth/core/utils/redirect-uri`
|
|
16
|
+
* rather than re-implementing the scheme policy per plugin.
|
|
17
|
+
*/
|
|
18
|
+
declare const SafeUrlSchema: z.ZodURL;
|
|
19
|
+
//#endregion
|
|
20
|
+
export { SafeUrlSchema };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { isLoopbackHost } from "./host.mjs";
|
|
2
|
+
import { DANGEROUS_URL_SCHEMES } from "./url.mjs";
|
|
3
|
+
import * as z from "zod";
|
|
4
|
+
//#region src/utils/redirect-uri.ts
|
|
5
|
+
/**
|
|
6
|
+
* Zod schema for OAuth redirect URIs and other developer-supplied URLs that the
|
|
7
|
+
* server stores and later hands back to a browser.
|
|
8
|
+
*
|
|
9
|
+
* - Rejects dangerous schemes (`javascript:`, `data:`, `vbscript:`).
|
|
10
|
+
* - Rejects URIs with a fragment component (`#...`) per RFC 6749 §3.1.2.
|
|
11
|
+
* - Requires HTTPS, except for loopback hosts (`127.0.0.0/8`, `[::1]`,
|
|
12
|
+
* `*.localhost` per RFC 6761), where HTTP is allowed for local development.
|
|
13
|
+
* - Allows custom schemes for mobile apps (e.g. `myapp://callback`).
|
|
14
|
+
*
|
|
15
|
+
* This is the single source of truth for redirect-URI validation across the
|
|
16
|
+
* OAuth provider plugins. Consume it from `@better-auth/core/utils/redirect-uri`
|
|
17
|
+
* rather than re-implementing the scheme policy per plugin.
|
|
18
|
+
*/
|
|
19
|
+
const SafeUrlSchema = z.url().superRefine((val, ctx) => {
|
|
20
|
+
let u;
|
|
21
|
+
try {
|
|
22
|
+
u = new URL(val);
|
|
23
|
+
} catch {
|
|
24
|
+
ctx.addIssue({
|
|
25
|
+
code: "custom",
|
|
26
|
+
message: "URL must be parseable",
|
|
27
|
+
fatal: true
|
|
28
|
+
});
|
|
29
|
+
return z.NEVER;
|
|
30
|
+
}
|
|
31
|
+
if (DANGEROUS_URL_SCHEMES.includes(u.protocol)) {
|
|
32
|
+
ctx.addIssue({
|
|
33
|
+
code: "custom",
|
|
34
|
+
message: "URL cannot use javascript:, data:, or vbscript: scheme"
|
|
35
|
+
});
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
if (val.includes("#")) ctx.addIssue({
|
|
39
|
+
code: "custom",
|
|
40
|
+
message: "Redirect URI must not contain a fragment component"
|
|
41
|
+
});
|
|
42
|
+
if (u.protocol === "http:" && !isLoopbackHost(u.host)) ctx.addIssue({
|
|
43
|
+
code: "custom",
|
|
44
|
+
message: "Redirect URI must use HTTPS (HTTP allowed only for loopback hosts)"
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
//#endregion
|
|
48
|
+
export { SafeUrlSchema };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
//#region src/utils/string.d.ts
|
|
2
|
+
declare function capitalizeFirstLetter(str: string): string;
|
|
3
|
+
declare function toSnakeCase(input: string): string;
|
|
4
|
+
declare function toKebabCase(input: string): string;
|
|
5
|
+
declare function toCamelCase(input: string): string;
|
|
6
|
+
declare function toPascalCase(input: string): string;
|
|
7
|
+
//#endregion
|
|
8
|
+
export { capitalizeFirstLetter, toCamelCase, toKebabCase, toPascalCase, toSnakeCase };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
//#region src/utils/string.ts
|
|
2
|
+
function capitalizeFirstLetter(str) {
|
|
3
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
4
|
+
}
|
|
5
|
+
const WORD_PATTERN = /[\p{Ll}\d]+|\p{Lu}+(?!\p{Ll})|\p{Lu}[\p{Ll}\d]+|\p{Lo}+/gu;
|
|
6
|
+
const APOSTROPHE_PATTERN = /['\u2019]/g;
|
|
7
|
+
function splitWords(input) {
|
|
8
|
+
return input.replace(APOSTROPHE_PATTERN, "").match(WORD_PATTERN) ?? [];
|
|
9
|
+
}
|
|
10
|
+
function toSnakeCase(input) {
|
|
11
|
+
return splitWords(input).map((word) => word.toLowerCase()).join("_");
|
|
12
|
+
}
|
|
13
|
+
function toKebabCase(input) {
|
|
14
|
+
return splitWords(input).map((word) => word.toLowerCase()).join("-");
|
|
15
|
+
}
|
|
16
|
+
function toCamelCase(input) {
|
|
17
|
+
return splitWords(input).reduce((acc, word, i) => {
|
|
18
|
+
return acc + (i === 0 ? word.toLowerCase() : `${word[0].toUpperCase()}${word.slice(1)}`);
|
|
19
|
+
}, "");
|
|
20
|
+
}
|
|
21
|
+
function toPascalCase(input) {
|
|
22
|
+
return splitWords(input).map((word) => `${word[0].toUpperCase()}${word.slice(1).toLowerCase()}`).join("");
|
|
23
|
+
}
|
|
24
|
+
//#endregion
|
|
25
|
+
export { capitalizeFirstLetter, toCamelCase, toKebabCase, toPascalCase, toSnakeCase };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
//#region src/utils/url.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Normalizes a request pathname by removing the basePath prefix and trailing slashes.
|
|
4
|
+
* This is useful for matching paths against configured path lists.
|
|
5
|
+
*
|
|
6
|
+
* @param requestUrl - The full request URL
|
|
7
|
+
* @param basePath - The base path of the auth API (e.g., "/api/auth")
|
|
8
|
+
* @returns The normalized path without basePath prefix or trailing slashes,
|
|
9
|
+
* or "/" if URL parsing fails
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* normalizePathname("http://localhost:3000/api/auth/sso/saml2/callback/provider1", "/api/auth")
|
|
13
|
+
* // Returns: "/sso/saml2/callback/provider1"
|
|
14
|
+
*
|
|
15
|
+
* normalizePathname("http://localhost:3000/sso/saml2/callback/provider1/", "/")
|
|
16
|
+
* // Returns: "/sso/saml2/callback/provider1"
|
|
17
|
+
*/
|
|
18
|
+
declare function normalizePathname(requestUrl: string, basePath: string): string;
|
|
19
|
+
/**
|
|
20
|
+
* Schemes that execute or embed code when navigated to or accepted as a
|
|
21
|
+
* redirect target. These are never safe as an OAuth `redirect_uri` or as a
|
|
22
|
+
* client-side navigation target (`window.location.href`, `location.assign`, ...).
|
|
23
|
+
*/
|
|
24
|
+
declare const DANGEROUS_URL_SCHEMES: string[];
|
|
25
|
+
/**
|
|
26
|
+
* Returns `false` only when `value` is an absolute URL using a dangerous scheme
|
|
27
|
+
* (`javascript:`, `data:`, `vbscript:`). Relative URLs (e.g. `/dashboard`) and
|
|
28
|
+
* safe absolute schemes (`http`, `https`, custom app schemes such as
|
|
29
|
+
* `myapp://`) return `true`.
|
|
30
|
+
*
|
|
31
|
+
* Use this to guard browser navigation sinks and any redirect target that may
|
|
32
|
+
* originate from untrusted input. It is intentionally narrow: it blocks code
|
|
33
|
+
* execution schemes without rejecting relative paths or mobile deep links.
|
|
34
|
+
*/
|
|
35
|
+
declare function isSafeUrlScheme(value: string): boolean;
|
|
36
|
+
//#endregion
|
|
37
|
+
export { DANGEROUS_URL_SCHEMES, isSafeUrlScheme, normalizePathname };
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
//#region src/utils/url.ts
|
|
2
|
+
/**
|
|
3
|
+
* Normalizes a request pathname by removing the basePath prefix and trailing slashes.
|
|
4
|
+
* This is useful for matching paths against configured path lists.
|
|
5
|
+
*
|
|
6
|
+
* @param requestUrl - The full request URL
|
|
7
|
+
* @param basePath - The base path of the auth API (e.g., "/api/auth")
|
|
8
|
+
* @returns The normalized path without basePath prefix or trailing slashes,
|
|
9
|
+
* or "/" if URL parsing fails
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* normalizePathname("http://localhost:3000/api/auth/sso/saml2/callback/provider1", "/api/auth")
|
|
13
|
+
* // Returns: "/sso/saml2/callback/provider1"
|
|
14
|
+
*
|
|
15
|
+
* normalizePathname("http://localhost:3000/sso/saml2/callback/provider1/", "/")
|
|
16
|
+
* // Returns: "/sso/saml2/callback/provider1"
|
|
17
|
+
*/
|
|
18
|
+
function normalizePathname(requestUrl, basePath) {
|
|
19
|
+
let pathname;
|
|
20
|
+
try {
|
|
21
|
+
pathname = new URL(requestUrl).pathname.replace(/\/+$/, "") || "/";
|
|
22
|
+
} catch {
|
|
23
|
+
return "/";
|
|
24
|
+
}
|
|
25
|
+
const normalizedBasePath = basePath.replace(/\/+$/, "");
|
|
26
|
+
if (normalizedBasePath === "") return pathname;
|
|
27
|
+
if (pathname === normalizedBasePath) return "/";
|
|
28
|
+
if (pathname.startsWith(normalizedBasePath + "/")) return pathname.slice(normalizedBasePath.length).replace(/\/+$/, "") || "/";
|
|
29
|
+
return pathname;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Schemes that execute or embed code when navigated to or accepted as a
|
|
33
|
+
* redirect target. These are never safe as an OAuth `redirect_uri` or as a
|
|
34
|
+
* client-side navigation target (`window.location.href`, `location.assign`, ...).
|
|
35
|
+
*/
|
|
36
|
+
const DANGEROUS_URL_SCHEMES = [
|
|
37
|
+
"javascript:",
|
|
38
|
+
"data:",
|
|
39
|
+
"vbscript:"
|
|
40
|
+
];
|
|
41
|
+
/**
|
|
42
|
+
* Returns `false` only when `value` is an absolute URL using a dangerous scheme
|
|
43
|
+
* (`javascript:`, `data:`, `vbscript:`). Relative URLs (e.g. `/dashboard`) and
|
|
44
|
+
* safe absolute schemes (`http`, `https`, custom app schemes such as
|
|
45
|
+
* `myapp://`) return `true`.
|
|
46
|
+
*
|
|
47
|
+
* Use this to guard browser navigation sinks and any redirect target that may
|
|
48
|
+
* originate from untrusted input. It is intentionally narrow: it blocks code
|
|
49
|
+
* execution schemes without rejecting relative paths or mobile deep links.
|
|
50
|
+
*/
|
|
51
|
+
function isSafeUrlScheme(value) {
|
|
52
|
+
let parsed;
|
|
53
|
+
try {
|
|
54
|
+
parsed = new URL(value);
|
|
55
|
+
} catch {
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
return !DANGEROUS_URL_SCHEMES.includes(parsed.protocol);
|
|
59
|
+
}
|
|
60
|
+
//#endregion
|
|
61
|
+
export { DANGEROUS_URL_SCHEMES, isSafeUrlScheme, normalizePathname };
|
package/package.json
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@depup/better-auth__core",
|
|
3
|
+
"version": "1.6.23-depup.2",
|
|
4
|
+
"description": "The most comprehensive authentication framework for TypeScript. (with updated dependencies)",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"homepage": "https://www.better-auth.com",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/better-auth/better-auth.git",
|
|
11
|
+
"directory": "packages/core"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"@better-auth/core",
|
|
15
|
+
"depup",
|
|
16
|
+
"updated-dependencies",
|
|
17
|
+
"security",
|
|
18
|
+
"latest",
|
|
19
|
+
"patched",
|
|
20
|
+
"auth",
|
|
21
|
+
"core",
|
|
22
|
+
"typescript",
|
|
23
|
+
"better-auth"
|
|
24
|
+
],
|
|
25
|
+
"sideEffects": false,
|
|
26
|
+
"files": [
|
|
27
|
+
"dist",
|
|
28
|
+
"src",
|
|
29
|
+
"!src/**/*.test.ts",
|
|
30
|
+
"changes.json",
|
|
31
|
+
"README.md"
|
|
32
|
+
],
|
|
33
|
+
"main": "./dist/index.mjs",
|
|
34
|
+
"module": "./dist/index.mjs",
|
|
35
|
+
"types": "./dist/index.d.mts",
|
|
36
|
+
"exports": {
|
|
37
|
+
".": {
|
|
38
|
+
"dev-source": "./src/index.ts",
|
|
39
|
+
"types": "./dist/index.d.mts",
|
|
40
|
+
"default": "./dist/index.mjs"
|
|
41
|
+
},
|
|
42
|
+
"./api": {
|
|
43
|
+
"dev-source": "./src/api/index.ts",
|
|
44
|
+
"types": "./dist/api/index.d.mts",
|
|
45
|
+
"default": "./dist/api/index.mjs"
|
|
46
|
+
},
|
|
47
|
+
"./async_hooks": {
|
|
48
|
+
"dev-source": "./src/async_hooks/index.ts",
|
|
49
|
+
"types": "./dist/async_hooks/index.d.mts",
|
|
50
|
+
"node": "./dist/async_hooks/index.mjs",
|
|
51
|
+
"deno": "./dist/async_hooks/index.mjs",
|
|
52
|
+
"bun": "./dist/async_hooks/index.mjs",
|
|
53
|
+
"edge": "./dist/async_hooks/pure.index.mjs",
|
|
54
|
+
"workerd": "./dist/async_hooks/index.mjs",
|
|
55
|
+
"browser": "./dist/async_hooks/pure.index.mjs",
|
|
56
|
+
"default": "./dist/async_hooks/index.mjs"
|
|
57
|
+
},
|
|
58
|
+
"./context": {
|
|
59
|
+
"dev-source": "./src/context/index.ts",
|
|
60
|
+
"types": "./dist/context/index.d.mts",
|
|
61
|
+
"default": "./dist/context/index.mjs"
|
|
62
|
+
},
|
|
63
|
+
"./env": {
|
|
64
|
+
"dev-source": "./src/env/index.ts",
|
|
65
|
+
"types": "./dist/env/index.d.mts",
|
|
66
|
+
"default": "./dist/env/index.mjs"
|
|
67
|
+
},
|
|
68
|
+
"./error": {
|
|
69
|
+
"dev-source": "./src/error/index.ts",
|
|
70
|
+
"types": "./dist/error/index.d.mts",
|
|
71
|
+
"default": "./dist/error/index.mjs"
|
|
72
|
+
},
|
|
73
|
+
"./utils/*": {
|
|
74
|
+
"dev-source": "./src/utils/*.ts",
|
|
75
|
+
"types": "./dist/utils/*.d.mts",
|
|
76
|
+
"default": "./dist/utils/*.mjs"
|
|
77
|
+
},
|
|
78
|
+
"./social-providers": {
|
|
79
|
+
"dev-source": "./src/social-providers/index.ts",
|
|
80
|
+
"types": "./dist/social-providers/index.d.mts",
|
|
81
|
+
"default": "./dist/social-providers/index.mjs"
|
|
82
|
+
},
|
|
83
|
+
"./db": {
|
|
84
|
+
"dev-source": "./src/db/index.ts",
|
|
85
|
+
"types": "./dist/db/index.d.mts",
|
|
86
|
+
"default": "./dist/db/index.mjs"
|
|
87
|
+
},
|
|
88
|
+
"./db/adapter": {
|
|
89
|
+
"dev-source": "./src/db/adapter/index.ts",
|
|
90
|
+
"types": "./dist/db/adapter/index.d.mts",
|
|
91
|
+
"default": "./dist/db/adapter/index.mjs"
|
|
92
|
+
},
|
|
93
|
+
"./oauth2": {
|
|
94
|
+
"dev-source": "./src/oauth2/index.ts",
|
|
95
|
+
"types": "./dist/oauth2/index.d.mts",
|
|
96
|
+
"default": "./dist/oauth2/index.mjs"
|
|
97
|
+
},
|
|
98
|
+
"./instrumentation": {
|
|
99
|
+
"dev-source": "./src/instrumentation/index.ts",
|
|
100
|
+
"types": "./dist/instrumentation/index.d.mts",
|
|
101
|
+
"workerd": "./dist/instrumentation/pure.index.mjs",
|
|
102
|
+
"edge": "./dist/instrumentation/pure.index.mjs",
|
|
103
|
+
"browser": "./dist/instrumentation/pure.index.mjs",
|
|
104
|
+
"node": "./dist/instrumentation/index.mjs",
|
|
105
|
+
"deno": "./dist/instrumentation/index.mjs",
|
|
106
|
+
"bun": "./dist/instrumentation/index.mjs",
|
|
107
|
+
"import": "./dist/instrumentation/index.mjs",
|
|
108
|
+
"default": "./dist/instrumentation/index.mjs"
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
"typesVersions": {
|
|
112
|
+
"*": {
|
|
113
|
+
".": [
|
|
114
|
+
"dist/index.d.mts"
|
|
115
|
+
],
|
|
116
|
+
"api": [
|
|
117
|
+
"dist/api/index.d.mts"
|
|
118
|
+
],
|
|
119
|
+
"async_hooks": [
|
|
120
|
+
"dist/async_hooks/index.d.mts"
|
|
121
|
+
],
|
|
122
|
+
"context": [
|
|
123
|
+
"dist/context/index.d.mts"
|
|
124
|
+
],
|
|
125
|
+
"env": [
|
|
126
|
+
"dist/env/index.d.mts"
|
|
127
|
+
],
|
|
128
|
+
"error": [
|
|
129
|
+
"dist/error/index.d.mts"
|
|
130
|
+
],
|
|
131
|
+
"utils": [
|
|
132
|
+
"dist/utils/index.d.mts"
|
|
133
|
+
],
|
|
134
|
+
"utils/*": [
|
|
135
|
+
"dist/utils/*.d.mts"
|
|
136
|
+
],
|
|
137
|
+
"social-providers": [
|
|
138
|
+
"dist/social-providers/index.d.mts"
|
|
139
|
+
],
|
|
140
|
+
"db": [
|
|
141
|
+
"dist/db/index.d.mts"
|
|
142
|
+
],
|
|
143
|
+
"db/adapter": [
|
|
144
|
+
"dist/db/adapter/index.d.mts"
|
|
145
|
+
],
|
|
146
|
+
"oauth2": [
|
|
147
|
+
"dist/oauth2/index.d.mts"
|
|
148
|
+
],
|
|
149
|
+
"instrumentation": [
|
|
150
|
+
"dist/instrumentation/index.d.mts"
|
|
151
|
+
]
|
|
152
|
+
}
|
|
153
|
+
},
|
|
154
|
+
"dependencies": {
|
|
155
|
+
"@opentelemetry/semantic-conventions": "^1.43.0",
|
|
156
|
+
"@standard-schema/spec": "^1.1.0",
|
|
157
|
+
"zod": "^4.4.3"
|
|
158
|
+
},
|
|
159
|
+
"devDependencies": {
|
|
160
|
+
"@better-auth/utils": "0.4.2",
|
|
161
|
+
"@better-fetch/fetch": "1.3.1",
|
|
162
|
+
"@opentelemetry/api": "^1.9.0",
|
|
163
|
+
"@opentelemetry/sdk-trace-base": "^1.30.0",
|
|
164
|
+
"@opentelemetry/sdk-trace-node": "^1.30.0",
|
|
165
|
+
"better-call": "1.3.7",
|
|
166
|
+
"@cloudflare/workers-types": "^4.20250121.0",
|
|
167
|
+
"jose": "^6.1.3",
|
|
168
|
+
"kysely": "^0.28.17 || ^0.29.0",
|
|
169
|
+
"nanostores": "^1.1.1",
|
|
170
|
+
"tsdown": "0.21.1"
|
|
171
|
+
},
|
|
172
|
+
"peerDependencies": {
|
|
173
|
+
"@better-auth/utils": "0.4.2",
|
|
174
|
+
"@better-fetch/fetch": "1.3.1",
|
|
175
|
+
"@opentelemetry/api": "^1.9.0",
|
|
176
|
+
"better-call": "1.3.7",
|
|
177
|
+
"@cloudflare/workers-types": ">=4",
|
|
178
|
+
"jose": "^6.1.0",
|
|
179
|
+
"kysely": "^0.28.5 || ^0.29.0",
|
|
180
|
+
"nanostores": "^1.0.1"
|
|
181
|
+
},
|
|
182
|
+
"peerDependenciesMeta": {
|
|
183
|
+
"@cloudflare/workers-types": {
|
|
184
|
+
"optional": true
|
|
185
|
+
},
|
|
186
|
+
"@opentelemetry/api": {
|
|
187
|
+
"optional": true
|
|
188
|
+
}
|
|
189
|
+
},
|
|
190
|
+
"scripts": {
|
|
191
|
+
"build": "tsdown",
|
|
192
|
+
"dev": "tsdown --watch",
|
|
193
|
+
"lint:package": "publint run --strict --pack false",
|
|
194
|
+
"lint:types": "attw --profile esm-only --pack .",
|
|
195
|
+
"typecheck": "tsc --project tsconfig.json",
|
|
196
|
+
"test": "vitest",
|
|
197
|
+
"coverage": "vitest run --coverage --coverage.provider=istanbul"
|
|
198
|
+
},
|
|
199
|
+
"depup": {
|
|
200
|
+
"changes": {
|
|
201
|
+
"@opentelemetry/semantic-conventions": {
|
|
202
|
+
"from": "^1.39.0",
|
|
203
|
+
"to": "^1.43.0"
|
|
204
|
+
},
|
|
205
|
+
"zod": {
|
|
206
|
+
"from": "^4.3.6",
|
|
207
|
+
"to": "^4.4.3"
|
|
208
|
+
}
|
|
209
|
+
},
|
|
210
|
+
"depsUpdated": 2,
|
|
211
|
+
"originalPackage": "@better-auth/core",
|
|
212
|
+
"originalVersion": "1.6.23",
|
|
213
|
+
"processedAt": "2026-07-21T16:15:02.120Z",
|
|
214
|
+
"smokeTest": "passed"
|
|
215
|
+
}
|
|
216
|
+
}
|
package/src/api/index.ts
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
EndpointContext,
|
|
3
|
+
EndpointOptions,
|
|
4
|
+
StrictEndpoint,
|
|
5
|
+
} from "better-call";
|
|
6
|
+
import {
|
|
7
|
+
createEndpoint,
|
|
8
|
+
createMiddleware,
|
|
9
|
+
kAPIErrorHeaderSymbol,
|
|
10
|
+
} from "better-call";
|
|
11
|
+
import { runWithEndpointContext } from "../context";
|
|
12
|
+
import type { AuthContext } from "../types";
|
|
13
|
+
import { isAPIError } from "../utils/is-api-error";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Better-call's createEndpoint re-throws APIError without exposing the headers
|
|
17
|
+
* accumulated on ctx.responseHeaders (e.g. Set-Cookie from deleteSessionCookie
|
|
18
|
+
* before throw). Attach them to the error via kAPIErrorHeaderSymbol — matching
|
|
19
|
+
* better-call's createMiddleware contract so the outer pipeline can merge them
|
|
20
|
+
* into the response.
|
|
21
|
+
*/
|
|
22
|
+
function attachResponseHeadersToAPIError(
|
|
23
|
+
responseHeaders: Headers | undefined,
|
|
24
|
+
e: unknown,
|
|
25
|
+
): void {
|
|
26
|
+
if (!isAPIError(e) || !responseHeaders) return;
|
|
27
|
+
Object.defineProperty(e, kAPIErrorHeaderSymbol, {
|
|
28
|
+
enumerable: false,
|
|
29
|
+
configurable: true,
|
|
30
|
+
value: responseHeaders,
|
|
31
|
+
writable: false,
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export const optionsMiddleware = createMiddleware(async () => {
|
|
36
|
+
/**
|
|
37
|
+
* This will be passed on the instance of
|
|
38
|
+
* the context. Used to infer the type
|
|
39
|
+
* here.
|
|
40
|
+
*/
|
|
41
|
+
return {} as AuthContext;
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
export const createAuthMiddleware = createMiddleware.create({
|
|
45
|
+
use: [
|
|
46
|
+
optionsMiddleware,
|
|
47
|
+
/**
|
|
48
|
+
* Only use for post hooks
|
|
49
|
+
*/
|
|
50
|
+
createMiddleware(async () => {
|
|
51
|
+
return {} as {
|
|
52
|
+
returned?: unknown | undefined;
|
|
53
|
+
responseHeaders?: Headers | undefined;
|
|
54
|
+
};
|
|
55
|
+
}),
|
|
56
|
+
],
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
const use = [optionsMiddleware];
|
|
60
|
+
|
|
61
|
+
type EndpointHandler<
|
|
62
|
+
Path extends string,
|
|
63
|
+
Options extends EndpointOptions,
|
|
64
|
+
R,
|
|
65
|
+
> = (context: EndpointContext<Path, Options, AuthContext>) => Promise<R>;
|
|
66
|
+
|
|
67
|
+
export function createAuthEndpoint<
|
|
68
|
+
Path extends string,
|
|
69
|
+
Options extends EndpointOptions,
|
|
70
|
+
R,
|
|
71
|
+
>(
|
|
72
|
+
path: Path,
|
|
73
|
+
options: Options,
|
|
74
|
+
handler: EndpointHandler<Path, Options, R>,
|
|
75
|
+
): StrictEndpoint<Path, Options, R>;
|
|
76
|
+
|
|
77
|
+
export function createAuthEndpoint<
|
|
78
|
+
Path extends string,
|
|
79
|
+
Options extends EndpointOptions,
|
|
80
|
+
R,
|
|
81
|
+
>(
|
|
82
|
+
options: Options,
|
|
83
|
+
handler: EndpointHandler<Path, Options, R>,
|
|
84
|
+
): StrictEndpoint<Path, Options, R>;
|
|
85
|
+
|
|
86
|
+
export function createAuthEndpoint<
|
|
87
|
+
Path extends string,
|
|
88
|
+
Opts extends EndpointOptions,
|
|
89
|
+
R,
|
|
90
|
+
>(
|
|
91
|
+
pathOrOptions: Path | Opts,
|
|
92
|
+
handlerOrOptions: EndpointHandler<Path, Opts, R> | Opts,
|
|
93
|
+
handlerOrNever?: any,
|
|
94
|
+
) {
|
|
95
|
+
const path: Path | undefined =
|
|
96
|
+
typeof pathOrOptions === "string" ? pathOrOptions : undefined;
|
|
97
|
+
const options: Opts =
|
|
98
|
+
typeof handlerOrOptions === "object"
|
|
99
|
+
? handlerOrOptions
|
|
100
|
+
: (pathOrOptions as Opts);
|
|
101
|
+
const handler: EndpointHandler<Path, Opts, R> =
|
|
102
|
+
typeof handlerOrOptions === "function" ? handlerOrOptions : handlerOrNever;
|
|
103
|
+
|
|
104
|
+
// todo: prettify the code, we want to call `runWithEndpointContext` to top level
|
|
105
|
+
const wrapped: EndpointHandler<Path, Opts, R> = async (ctx) => {
|
|
106
|
+
const runtimeCtx = ctx as unknown as { responseHeaders?: Headers };
|
|
107
|
+
try {
|
|
108
|
+
return await runWithEndpointContext(ctx as any, () => handler(ctx));
|
|
109
|
+
} catch (e) {
|
|
110
|
+
attachResponseHeadersToAPIError(runtimeCtx.responseHeaders, e);
|
|
111
|
+
throw e;
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
if (path) {
|
|
116
|
+
return createEndpoint(
|
|
117
|
+
path,
|
|
118
|
+
{
|
|
119
|
+
...options,
|
|
120
|
+
use: [...(options?.use || []), ...use],
|
|
121
|
+
},
|
|
122
|
+
wrapped,
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return createEndpoint(
|
|
127
|
+
{
|
|
128
|
+
...options,
|
|
129
|
+
use: [...(options?.use || []), ...use],
|
|
130
|
+
},
|
|
131
|
+
wrapped,
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Set `metadata.SERVER_ONLY` while preserving any existing metadata
|
|
137
|
+
* (`$Infer`, `openapi`, ...).
|
|
138
|
+
*/
|
|
139
|
+
function withServerOnly<Options extends EndpointOptions>(
|
|
140
|
+
options: Options,
|
|
141
|
+
): Options {
|
|
142
|
+
return {
|
|
143
|
+
...options,
|
|
144
|
+
metadata: { ...options.metadata, SERVER_ONLY: true },
|
|
145
|
+
} as Options;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Declare a **server-only** endpoint.
|
|
150
|
+
*
|
|
151
|
+
* The endpoint is callable through `auth.api.*` from trusted server code but is
|
|
152
|
+
* never registered on the HTTP router and never emitted into the OpenAPI
|
|
153
|
+
* schema. It takes no path because it has no URL to be reached at.
|
|
154
|
+
*
|
|
155
|
+
* Prefer this over the path-less `createAuthEndpoint({ ... }, handler)` form.
|
|
156
|
+
* Setting `metadata.SERVER_ONLY` makes the intent explicit at the call site and
|
|
157
|
+
* keeps the endpoint off the HTTP surface even if a path is later added by
|
|
158
|
+
* mistake: better-call's router skips an endpoint when its path is missing *or*
|
|
159
|
+
* when `SERVER_ONLY` is set, so the two together are defense in depth. Relying
|
|
160
|
+
* on path omission alone is invisible and one keystroke away from exposure.
|
|
161
|
+
*
|
|
162
|
+
* @example
|
|
163
|
+
* ```ts
|
|
164
|
+
* viewBackupCodes: createAuthEndpoint.serverOnly(
|
|
165
|
+
* { method: "POST", body: schema },
|
|
166
|
+
* async (ctx) => { ... },
|
|
167
|
+
* )
|
|
168
|
+
* ```
|
|
169
|
+
*/
|
|
170
|
+
createAuthEndpoint.serverOnly = <
|
|
171
|
+
Path extends string,
|
|
172
|
+
Options extends EndpointOptions,
|
|
173
|
+
R,
|
|
174
|
+
>(
|
|
175
|
+
options: Options,
|
|
176
|
+
handler: EndpointHandler<Path, Options, R>,
|
|
177
|
+
): StrictEndpoint<Path, Options, R> =>
|
|
178
|
+
createAuthEndpoint(withServerOnly(options), handler);
|
|
179
|
+
|
|
180
|
+
export type AuthEndpoint<
|
|
181
|
+
Path extends string,
|
|
182
|
+
Opts extends EndpointOptions,
|
|
183
|
+
R,
|
|
184
|
+
> = ReturnType<typeof createAuthEndpoint<Path, Opts, R>>;
|
|
185
|
+
export type AuthMiddleware = ReturnType<typeof createAuthMiddleware>;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
|
|
3
|
+
export type { AsyncLocalStorage };
|
|
4
|
+
|
|
5
|
+
const AsyncLocalStoragePromise: Promise<typeof AsyncLocalStorage | null> =
|
|
6
|
+
import(
|
|
7
|
+
/* @vite-ignore */
|
|
8
|
+
/* webpackIgnore: true */
|
|
9
|
+
"node:async_hooks"
|
|
10
|
+
)
|
|
11
|
+
.then((mod) => mod.AsyncLocalStorage)
|
|
12
|
+
.catch((err) => {
|
|
13
|
+
if ("AsyncLocalStorage" in globalThis) {
|
|
14
|
+
return (globalThis as any).AsyncLocalStorage;
|
|
15
|
+
}
|
|
16
|
+
if (typeof window !== "undefined") {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
console.warn(
|
|
20
|
+
"[better-auth] Warning: AsyncLocalStorage is not available in this environment. Some features may not work as expected.",
|
|
21
|
+
);
|
|
22
|
+
console.warn(
|
|
23
|
+
"[better-auth] Please read more about this warning at https://better-auth.com/docs/installation#mount-handler",
|
|
24
|
+
);
|
|
25
|
+
console.warn(
|
|
26
|
+
"[better-auth] If you are using Cloudflare Workers, please see: https://developers.cloudflare.com/workers/configuration/compatibility-flags/#nodejs-compatibility-flag",
|
|
27
|
+
);
|
|
28
|
+
throw err;
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
export async function getAsyncLocalStorage(): Promise<
|
|
32
|
+
typeof AsyncLocalStorage
|
|
33
|
+
> {
|
|
34
|
+
const mod = await AsyncLocalStoragePromise;
|
|
35
|
+
if (mod === null) {
|
|
36
|
+
throw new Error("getAsyncLocalStorage is only available in server code");
|
|
37
|
+
} else {
|
|
38
|
+
return mod;
|
|
39
|
+
}
|
|
40
|
+
}
|