@better-auth/core 1.5.6 → 1.6.0-beta.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/api/index.d.mts +42 -13
- package/dist/api/index.mjs +0 -4
- package/dist/async_hooks/index.d.mts +1 -2
- package/dist/async_hooks/index.mjs +0 -2
- package/dist/async_hooks/pure.index.d.mts +1 -2
- package/dist/async_hooks/pure.index.mjs +0 -2
- package/dist/context/endpoint-context.d.mts +2 -3
- package/dist/context/endpoint-context.mjs +0 -3
- package/dist/context/global.d.mts +1 -2
- package/dist/context/global.mjs +1 -3
- package/dist/context/index.mjs +1 -2
- package/dist/context/request-state.d.mts +1 -2
- package/dist/context/request-state.mjs +0 -3
- package/dist/context/transaction.d.mts +1 -2
- package/dist/context/transaction.mjs +0 -3
- package/dist/db/adapter/factory.d.mts +1 -2
- package/dist/db/adapter/factory.mjs +3 -7
- package/dist/db/adapter/get-default-field-name.d.mts +1 -2
- package/dist/db/adapter/get-default-field-name.mjs +0 -3
- package/dist/db/adapter/get-default-model-name.d.mts +1 -2
- package/dist/db/adapter/get-default-model-name.mjs +0 -3
- package/dist/db/adapter/get-field-attributes.d.mts +1 -2
- package/dist/db/adapter/get-field-attributes.mjs +0 -3
- package/dist/db/adapter/get-field-name.d.mts +1 -2
- package/dist/db/adapter/get-field-name.mjs +0 -3
- package/dist/db/adapter/get-id-field.d.mts +1 -2
- package/dist/db/adapter/get-id-field.mjs +0 -4
- package/dist/db/adapter/get-model-name.d.mts +1 -2
- package/dist/db/adapter/get-model-name.mjs +0 -3
- package/dist/db/adapter/index.d.mts +9 -2
- package/dist/db/adapter/index.mjs +0 -3
- package/dist/db/adapter/types.d.mts +1 -2
- package/dist/db/adapter/utils.d.mts +1 -2
- package/dist/db/adapter/utils.mjs +0 -2
- package/dist/db/get-tables.d.mts +1 -2
- package/dist/db/get-tables.mjs +0 -2
- package/dist/db/index.mjs +1 -2
- package/dist/db/plugin.d.mts +1 -2
- package/dist/db/schema/account.d.mts +1 -2
- package/dist/db/schema/account.mjs +0 -3
- package/dist/db/schema/rate-limit.d.mts +1 -2
- package/dist/db/schema/rate-limit.mjs +0 -3
- package/dist/db/schema/session.d.mts +1 -2
- package/dist/db/schema/session.mjs +0 -3
- package/dist/db/schema/shared.d.mts +1 -2
- package/dist/db/schema/shared.mjs +0 -3
- package/dist/db/schema/user.d.mts +1 -2
- package/dist/db/schema/user.mjs +0 -3
- package/dist/db/schema/verification.d.mts +1 -2
- package/dist/db/schema/verification.mjs +0 -3
- package/dist/db/type.d.mts +1 -2
- package/dist/env/color-depth.d.mts +1 -2
- package/dist/env/color-depth.mjs +0 -3
- package/dist/env/env-impl.d.mts +1 -2
- package/dist/env/env-impl.mjs +0 -2
- package/dist/env/index.mjs +1 -2
- package/dist/env/logger.d.mts +1 -2
- package/dist/env/logger.mjs +0 -3
- package/dist/error/codes.d.mts +1 -2
- package/dist/error/codes.mjs +0 -3
- package/dist/error/index.d.mts +1 -2
- package/dist/error/index.mjs +0 -3
- package/dist/index.mjs +1 -1
- package/dist/instrumentation/attributes.d.mts +1 -2
- package/dist/instrumentation/attributes.mjs +0 -3
- package/dist/instrumentation/index.mjs +1 -2
- package/dist/instrumentation/tracer.d.mts +1 -2
- package/dist/instrumentation/tracer.mjs +29 -16
- package/dist/oauth2/client-credentials-token.d.mts +1 -2
- package/dist/oauth2/client-credentials-token.mjs +0 -3
- package/dist/oauth2/create-authorization-url.d.mts +1 -2
- package/dist/oauth2/create-authorization-url.mjs +0 -3
- package/dist/oauth2/index.mjs +1 -2
- package/dist/oauth2/oauth-provider.d.mts +1 -2
- package/dist/oauth2/refresh-access-token.d.mts +1 -2
- package/dist/oauth2/refresh-access-token.mjs +0 -3
- package/dist/oauth2/utils.d.mts +1 -2
- package/dist/oauth2/utils.mjs +0 -3
- package/dist/oauth2/validate-authorization-code.d.mts +1 -2
- package/dist/oauth2/validate-authorization-code.mjs +0 -4
- package/dist/oauth2/verify.d.mts +1 -2
- package/dist/oauth2/verify.mjs +0 -4
- package/dist/social-providers/apple.d.mts +1 -2
- package/dist/social-providers/apple.mjs +0 -4
- package/dist/social-providers/atlassian.d.mts +1 -2
- package/dist/social-providers/atlassian.mjs +0 -5
- package/dist/social-providers/cognito.d.mts +1 -2
- package/dist/social-providers/cognito.mjs +0 -5
- package/dist/social-providers/discord.d.mts +1 -2
- package/dist/social-providers/discord.mjs +0 -4
- package/dist/social-providers/dropbox.d.mts +1 -2
- package/dist/social-providers/dropbox.mjs +0 -4
- package/dist/social-providers/facebook.d.mts +1 -2
- package/dist/social-providers/facebook.mjs +0 -4
- package/dist/social-providers/figma.d.mts +1 -2
- package/dist/social-providers/figma.mjs +0 -5
- package/dist/social-providers/github.d.mts +1 -2
- package/dist/social-providers/github.mjs +0 -5
- package/dist/social-providers/gitlab.d.mts +1 -2
- package/dist/social-providers/gitlab.mjs +0 -4
- package/dist/social-providers/google.d.mts +1 -2
- package/dist/social-providers/google.mjs +0 -5
- package/dist/social-providers/huggingface.d.mts +1 -2
- package/dist/social-providers/huggingface.mjs +0 -4
- package/dist/social-providers/index.d.mts +1 -2
- package/dist/social-providers/index.mjs +0 -3
- package/dist/social-providers/kakao.d.mts +1 -2
- package/dist/social-providers/kakao.mjs +0 -4
- package/dist/social-providers/kick.d.mts +1 -2
- package/dist/social-providers/kick.mjs +0 -4
- package/dist/social-providers/line.d.mts +1 -2
- package/dist/social-providers/line.mjs +0 -4
- package/dist/social-providers/linear.d.mts +1 -2
- package/dist/social-providers/linear.mjs +0 -4
- package/dist/social-providers/linkedin.d.mts +1 -2
- package/dist/social-providers/linkedin.mjs +0 -4
- package/dist/social-providers/microsoft-entra-id.d.mts +1 -2
- package/dist/social-providers/microsoft-entra-id.mjs +0 -5
- package/dist/social-providers/naver.d.mts +1 -2
- package/dist/social-providers/naver.mjs +0 -4
- package/dist/social-providers/notion.d.mts +1 -2
- package/dist/social-providers/notion.mjs +0 -4
- package/dist/social-providers/paybin.d.mts +1 -2
- package/dist/social-providers/paybin.mjs +0 -5
- package/dist/social-providers/paypal.d.mts +1 -2
- package/dist/social-providers/paypal.mjs +0 -5
- package/dist/social-providers/polar.d.mts +1 -2
- package/dist/social-providers/polar.mjs +0 -4
- package/dist/social-providers/railway.d.mts +1 -2
- package/dist/social-providers/railway.mjs +0 -4
- package/dist/social-providers/reddit.d.mts +1 -2
- package/dist/social-providers/reddit.mjs +0 -4
- package/dist/social-providers/roblox.d.mts +1 -2
- package/dist/social-providers/roblox.mjs +0 -4
- package/dist/social-providers/salesforce.d.mts +1 -2
- package/dist/social-providers/salesforce.mjs +0 -5
- package/dist/social-providers/slack.d.mts +1 -2
- package/dist/social-providers/slack.mjs +0 -4
- package/dist/social-providers/spotify.d.mts +1 -2
- package/dist/social-providers/spotify.mjs +0 -4
- package/dist/social-providers/tiktok.d.mts +1 -2
- package/dist/social-providers/tiktok.mjs +0 -4
- package/dist/social-providers/twitch.d.mts +1 -2
- package/dist/social-providers/twitch.mjs +0 -5
- package/dist/social-providers/twitter.d.mts +1 -2
- package/dist/social-providers/twitter.mjs +0 -4
- package/dist/social-providers/vercel.d.mts +1 -2
- package/dist/social-providers/vercel.mjs +0 -4
- package/dist/social-providers/vk.d.mts +1 -2
- package/dist/social-providers/vk.mjs +0 -4
- package/dist/social-providers/wechat.d.mts +1 -2
- package/dist/social-providers/wechat.mjs +0 -3
- package/dist/social-providers/zoom.d.mts +1 -2
- package/dist/social-providers/zoom.mjs +0 -4
- package/dist/types/context.d.mts +3 -5
- package/dist/types/cookie.d.mts +1 -2
- package/dist/types/helper.d.mts +1 -2
- package/dist/types/init-options.d.mts +14 -6
- package/dist/types/plugin-client.d.mts +2 -2
- package/dist/types/plugin.d.mts +6 -3
- package/dist/types/secret.d.mts +1 -2
- package/dist/utils/db.d.mts +1 -2
- package/dist/utils/db.mjs +0 -2
- package/dist/utils/deprecate.d.mts +1 -2
- package/dist/utils/deprecate.mjs +0 -2
- package/dist/utils/error-codes.d.mts +1 -2
- package/dist/utils/error-codes.mjs +0 -2
- package/dist/utils/fetch-metadata.d.mts +1 -2
- package/dist/utils/fetch-metadata.mjs +0 -2
- package/dist/utils/id.d.mts +1 -2
- package/dist/utils/id.mjs +0 -3
- package/dist/utils/ip.d.mts +1 -2
- package/dist/utils/ip.mjs +0 -3
- package/dist/utils/json.d.mts +1 -2
- package/dist/utils/json.mjs +0 -4
- package/dist/utils/string.d.mts +1 -2
- package/dist/utils/string.mjs +0 -2
- package/dist/utils/url.d.mts +1 -2
- package/dist/utils/url.mjs +0 -2
- package/package.json +11 -9
- package/src/api/index.ts +151 -41
- package/src/context/endpoint-context.ts +2 -1
- package/src/db/adapter/factory.ts +2 -0
- package/src/db/adapter/index.ts +8 -0
- package/src/instrumentation/tracer.ts +40 -12
- package/src/social-providers/index.ts +0 -2
- package/src/types/context.ts +2 -3
- package/src/types/init-options.ts +13 -4
- package/src/types/plugin-client.ts +1 -0
- package/src/types/plugin.ts +15 -1
- package/dist/api/index.mjs.map +0 -1
- package/dist/async_hooks/index.mjs.map +0 -1
- package/dist/async_hooks/pure.index.mjs.map +0 -1
- package/dist/context/endpoint-context.mjs.map +0 -1
- package/dist/context/global.mjs.map +0 -1
- package/dist/context/request-state.mjs.map +0 -1
- package/dist/context/transaction.mjs.map +0 -1
- package/dist/db/adapter/factory.mjs.map +0 -1
- package/dist/db/adapter/get-default-field-name.mjs.map +0 -1
- package/dist/db/adapter/get-default-model-name.mjs.map +0 -1
- package/dist/db/adapter/get-field-attributes.mjs.map +0 -1
- package/dist/db/adapter/get-field-name.mjs.map +0 -1
- package/dist/db/adapter/get-id-field.mjs.map +0 -1
- package/dist/db/adapter/get-model-name.mjs.map +0 -1
- package/dist/db/adapter/index.mjs.map +0 -1
- package/dist/db/adapter/utils.mjs.map +0 -1
- package/dist/db/get-tables.mjs.map +0 -1
- package/dist/db/schema/account.mjs.map +0 -1
- package/dist/db/schema/rate-limit.mjs.map +0 -1
- package/dist/db/schema/session.mjs.map +0 -1
- package/dist/db/schema/shared.mjs.map +0 -1
- package/dist/db/schema/user.mjs.map +0 -1
- package/dist/db/schema/verification.mjs.map +0 -1
- package/dist/env/color-depth.mjs.map +0 -1
- package/dist/env/env-impl.mjs.map +0 -1
- package/dist/env/logger.mjs.map +0 -1
- package/dist/error/codes.mjs.map +0 -1
- package/dist/error/index.mjs.map +0 -1
- package/dist/instrumentation/attributes.mjs.map +0 -1
- package/dist/instrumentation/tracer.mjs.map +0 -1
- package/dist/oauth2/client-credentials-token.mjs.map +0 -1
- package/dist/oauth2/create-authorization-url.mjs.map +0 -1
- package/dist/oauth2/refresh-access-token.mjs.map +0 -1
- package/dist/oauth2/utils.mjs.map +0 -1
- package/dist/oauth2/validate-authorization-code.mjs.map +0 -1
- package/dist/oauth2/verify.mjs.map +0 -1
- package/dist/social-providers/apple.mjs.map +0 -1
- package/dist/social-providers/atlassian.mjs.map +0 -1
- package/dist/social-providers/cognito.mjs.map +0 -1
- package/dist/social-providers/discord.mjs.map +0 -1
- package/dist/social-providers/dropbox.mjs.map +0 -1
- package/dist/social-providers/facebook.mjs.map +0 -1
- package/dist/social-providers/figma.mjs.map +0 -1
- package/dist/social-providers/github.mjs.map +0 -1
- package/dist/social-providers/gitlab.mjs.map +0 -1
- package/dist/social-providers/google.mjs.map +0 -1
- package/dist/social-providers/huggingface.mjs.map +0 -1
- package/dist/social-providers/index.mjs.map +0 -1
- package/dist/social-providers/kakao.mjs.map +0 -1
- package/dist/social-providers/kick.mjs.map +0 -1
- package/dist/social-providers/line.mjs.map +0 -1
- package/dist/social-providers/linear.mjs.map +0 -1
- package/dist/social-providers/linkedin.mjs.map +0 -1
- package/dist/social-providers/microsoft-entra-id.mjs.map +0 -1
- package/dist/social-providers/naver.mjs.map +0 -1
- package/dist/social-providers/notion.mjs.map +0 -1
- package/dist/social-providers/paybin.mjs.map +0 -1
- package/dist/social-providers/paypal.mjs.map +0 -1
- package/dist/social-providers/polar.mjs.map +0 -1
- package/dist/social-providers/railway.mjs.map +0 -1
- package/dist/social-providers/reddit.mjs.map +0 -1
- package/dist/social-providers/roblox.mjs.map +0 -1
- package/dist/social-providers/salesforce.mjs.map +0 -1
- package/dist/social-providers/slack.mjs.map +0 -1
- package/dist/social-providers/spotify.mjs.map +0 -1
- package/dist/social-providers/tiktok.mjs.map +0 -1
- package/dist/social-providers/twitch.mjs.map +0 -1
- package/dist/social-providers/twitter.mjs.map +0 -1
- package/dist/social-providers/vercel.mjs.map +0 -1
- package/dist/social-providers/vk.mjs.map +0 -1
- package/dist/social-providers/wechat.mjs.map +0 -1
- package/dist/social-providers/zoom.mjs.map +0 -1
- package/dist/utils/db.mjs.map +0 -1
- package/dist/utils/deprecate.mjs.map +0 -1
- package/dist/utils/error-codes.mjs.map +0 -1
- package/dist/utils/fetch-metadata.mjs.map +0 -1
- package/dist/utils/id.mjs.map +0 -1
- package/dist/utils/ip.mjs.map +0 -1
- package/dist/utils/json.mjs.map +0 -1
- package/dist/utils/string.mjs.map +0 -1
- package/dist/utils/url.mjs.map +0 -1
- package/src/context/request-state.test.ts +0 -94
- package/src/db/adapter/get-id-field.test.ts +0 -222
- package/src/db/test/get-tables.test.ts +0 -116
- package/src/env/logger.test.ts +0 -34
- package/src/instrumentation/instrumentation.test.ts +0 -139
- package/src/oauth2/refresh-access-token.test.ts +0 -90
- package/src/oauth2/validate-token.test.ts +0 -229
- package/src/utils/deprecate.test.ts +0 -71
- package/src/utils/fetch-metadata.test.ts +0 -28
- package/src/utils/ip.test.ts +0 -255
package/src/api/index.ts
CHANGED
|
@@ -1,7 +1,15 @@
|
|
|
1
1
|
import type {
|
|
2
|
+
Endpoint,
|
|
2
3
|
EndpointContext,
|
|
3
|
-
|
|
4
|
-
|
|
4
|
+
EndpointMetadata,
|
|
5
|
+
EndpointRuntimeOptions,
|
|
6
|
+
HTTPMethod,
|
|
7
|
+
Middleware,
|
|
8
|
+
ResolveBodyInput,
|
|
9
|
+
ResolveErrorInput,
|
|
10
|
+
ResolveMetaInput,
|
|
11
|
+
ResolveQueryInput,
|
|
12
|
+
StandardSchemaV1,
|
|
5
13
|
} from "better-call";
|
|
6
14
|
import { createEndpoint, createMiddleware } from "better-call";
|
|
7
15
|
import { runWithEndpointContext } from "../context";
|
|
@@ -33,47 +41,145 @@ export const createAuthMiddleware = createMiddleware.create({
|
|
|
33
41
|
|
|
34
42
|
const use = [optionsMiddleware];
|
|
35
43
|
|
|
36
|
-
type
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
44
|
+
type BodyOption<M, B extends object | undefined = undefined> = M extends
|
|
45
|
+
| "GET"
|
|
46
|
+
| "HEAD"
|
|
47
|
+
| ("GET" | "HEAD")[]
|
|
48
|
+
? { body?: never }
|
|
49
|
+
: { body?: B };
|
|
50
|
+
|
|
51
|
+
type AuthEndpointOptions<
|
|
52
|
+
Method extends HTTPMethod | HTTPMethod[] | "*",
|
|
53
|
+
BodySchema extends object | undefined,
|
|
54
|
+
QuerySchema extends object | undefined,
|
|
55
|
+
Use extends Middleware[],
|
|
56
|
+
ReqHeaders extends boolean,
|
|
57
|
+
ReqRequest extends boolean,
|
|
58
|
+
Meta extends EndpointMetadata | undefined,
|
|
59
|
+
ErrorSchema extends StandardSchemaV1 | undefined = undefined,
|
|
60
|
+
> = { method: Method } & BodyOption<Method, BodySchema> & {
|
|
61
|
+
query?: QuerySchema;
|
|
62
|
+
use?: [...Use];
|
|
63
|
+
requireHeaders?: ReqHeaders;
|
|
64
|
+
requireRequest?: ReqRequest;
|
|
65
|
+
error?: ErrorSchema;
|
|
66
|
+
cloneRequest?: boolean;
|
|
67
|
+
disableBody?: boolean;
|
|
68
|
+
metadata?: Meta;
|
|
69
|
+
[key: string]: any;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Normalize readonly tuples produced by `const` type parameters
|
|
74
|
+
* into mutable arrays so downstream `M extends Array<any>` checks work.
|
|
75
|
+
*/
|
|
76
|
+
type NormalizeMethod<M> = M extends readonly (infer E)[] ? E[] : M;
|
|
41
77
|
|
|
78
|
+
// Path + options + handler overload
|
|
42
79
|
export function createAuthEndpoint<
|
|
43
80
|
Path extends string,
|
|
44
|
-
|
|
45
|
-
|
|
81
|
+
const Method extends HTTPMethod | HTTPMethod[] | "*",
|
|
82
|
+
BodySchema extends object | undefined = undefined,
|
|
83
|
+
QuerySchema extends object | undefined = undefined,
|
|
84
|
+
Use extends Middleware[] = [],
|
|
85
|
+
ReqHeaders extends boolean = false,
|
|
86
|
+
ReqRequest extends boolean = false,
|
|
87
|
+
R = unknown,
|
|
88
|
+
Meta extends EndpointMetadata | undefined = undefined,
|
|
89
|
+
ErrorSchema extends StandardSchemaV1 | undefined = undefined,
|
|
46
90
|
>(
|
|
47
91
|
path: Path,
|
|
48
|
-
options:
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
92
|
+
options: AuthEndpointOptions<
|
|
93
|
+
Method,
|
|
94
|
+
BodySchema,
|
|
95
|
+
QuerySchema,
|
|
96
|
+
Use,
|
|
97
|
+
ReqHeaders,
|
|
98
|
+
ReqRequest,
|
|
99
|
+
Meta,
|
|
100
|
+
ErrorSchema
|
|
101
|
+
>,
|
|
102
|
+
handler: (
|
|
103
|
+
ctx: EndpointContext<
|
|
104
|
+
Path,
|
|
105
|
+
Method,
|
|
106
|
+
BodySchema,
|
|
107
|
+
QuerySchema,
|
|
108
|
+
Use,
|
|
109
|
+
ReqHeaders,
|
|
110
|
+
ReqRequest,
|
|
111
|
+
AuthContext,
|
|
112
|
+
Meta
|
|
113
|
+
>,
|
|
114
|
+
) => Promise<R>,
|
|
115
|
+
): Endpoint<
|
|
116
|
+
Path,
|
|
117
|
+
NormalizeMethod<Method>,
|
|
118
|
+
ResolveBodyInput<BodySchema, Meta>,
|
|
119
|
+
ResolveQueryInput<QuerySchema, Meta>,
|
|
120
|
+
Use,
|
|
55
121
|
R,
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
): StrictEndpoint<Path, Options, R>;
|
|
122
|
+
ResolveMetaInput<Meta>,
|
|
123
|
+
ResolveErrorInput<ErrorSchema, Meta>
|
|
124
|
+
>;
|
|
60
125
|
|
|
126
|
+
// Options-only (virtual/path-less) overload
|
|
61
127
|
export function createAuthEndpoint<
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
128
|
+
const Method extends HTTPMethod | HTTPMethod[] | "*",
|
|
129
|
+
BodySchema extends object | undefined = undefined,
|
|
130
|
+
QuerySchema extends object | undefined = undefined,
|
|
131
|
+
Use extends Middleware[] = [],
|
|
132
|
+
ReqHeaders extends boolean = false,
|
|
133
|
+
ReqRequest extends boolean = false,
|
|
134
|
+
R = unknown,
|
|
135
|
+
Meta extends EndpointMetadata | undefined = undefined,
|
|
136
|
+
ErrorSchema extends StandardSchemaV1 | undefined = undefined,
|
|
65
137
|
>(
|
|
66
|
-
|
|
67
|
-
|
|
138
|
+
options: AuthEndpointOptions<
|
|
139
|
+
Method,
|
|
140
|
+
BodySchema,
|
|
141
|
+
QuerySchema,
|
|
142
|
+
Use,
|
|
143
|
+
ReqHeaders,
|
|
144
|
+
ReqRequest,
|
|
145
|
+
Meta,
|
|
146
|
+
ErrorSchema
|
|
147
|
+
>,
|
|
148
|
+
handler: (
|
|
149
|
+
ctx: EndpointContext<
|
|
150
|
+
string,
|
|
151
|
+
Method,
|
|
152
|
+
BodySchema,
|
|
153
|
+
QuerySchema,
|
|
154
|
+
Use,
|
|
155
|
+
ReqHeaders,
|
|
156
|
+
ReqRequest,
|
|
157
|
+
AuthContext,
|
|
158
|
+
Meta
|
|
159
|
+
>,
|
|
160
|
+
) => Promise<R>,
|
|
161
|
+
): Endpoint<
|
|
162
|
+
string,
|
|
163
|
+
NormalizeMethod<Method>,
|
|
164
|
+
ResolveBodyInput<BodySchema, Meta>,
|
|
165
|
+
ResolveQueryInput<QuerySchema, Meta>,
|
|
166
|
+
Use,
|
|
167
|
+
R,
|
|
168
|
+
ResolveMetaInput<Meta>,
|
|
169
|
+
ResolveErrorInput<ErrorSchema, Meta>
|
|
170
|
+
>;
|
|
171
|
+
|
|
172
|
+
// Implementation
|
|
173
|
+
export function createAuthEndpoint(
|
|
174
|
+
pathOrOptions: any,
|
|
175
|
+
handlerOrOptions: any,
|
|
68
176
|
handlerOrNever?: any,
|
|
69
177
|
) {
|
|
70
|
-
const path:
|
|
178
|
+
const path: string | undefined =
|
|
71
179
|
typeof pathOrOptions === "string" ? pathOrOptions : undefined;
|
|
72
|
-
const options:
|
|
73
|
-
typeof handlerOrOptions === "object"
|
|
74
|
-
|
|
75
|
-
: (pathOrOptions as Opts);
|
|
76
|
-
const handler: EndpointHandler<Path, Opts, R> =
|
|
180
|
+
const options: EndpointRuntimeOptions =
|
|
181
|
+
typeof handlerOrOptions === "object" ? handlerOrOptions : pathOrOptions;
|
|
182
|
+
const handler =
|
|
77
183
|
typeof handlerOrOptions === "function" ? handlerOrOptions : handlerOrNever;
|
|
78
184
|
|
|
79
185
|
if (path) {
|
|
@@ -82,9 +188,9 @@ export function createAuthEndpoint<
|
|
|
82
188
|
{
|
|
83
189
|
...options,
|
|
84
190
|
use: [...(options?.use || []), ...use],
|
|
85
|
-
},
|
|
191
|
+
} as any,
|
|
86
192
|
// todo: prettify the code, we want to call `runWithEndpointContext` to top level
|
|
87
|
-
async (ctx) => runWithEndpointContext(ctx
|
|
193
|
+
async (ctx: any) => runWithEndpointContext(ctx, () => handler(ctx)),
|
|
88
194
|
);
|
|
89
195
|
}
|
|
90
196
|
|
|
@@ -92,15 +198,19 @@ export function createAuthEndpoint<
|
|
|
92
198
|
{
|
|
93
199
|
...options,
|
|
94
200
|
use: [...(options?.use || []), ...use],
|
|
95
|
-
},
|
|
201
|
+
} as any,
|
|
96
202
|
// todo: prettify the code, we want to call `runWithEndpointContext` to top level
|
|
97
|
-
async (ctx) => runWithEndpointContext(ctx
|
|
203
|
+
async (ctx: any) => runWithEndpointContext(ctx, () => handler(ctx)),
|
|
98
204
|
);
|
|
99
205
|
}
|
|
100
206
|
|
|
101
|
-
export type AuthEndpoint<
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
207
|
+
export type AuthEndpoint = ReturnType<typeof createAuthEndpoint>;
|
|
208
|
+
/**
|
|
209
|
+
* The handler type for plugin hooks.
|
|
210
|
+
*
|
|
211
|
+
* Accepts both `Middleware` instances (from `createAuthMiddleware`)
|
|
212
|
+
* and plain async functions for better-call v1/v2 compatibility.
|
|
213
|
+
*/
|
|
214
|
+
export type AuthMiddleware = (
|
|
215
|
+
inputContext: Record<string, any>,
|
|
216
|
+
) => Promise<unknown>;
|
|
@@ -5,7 +5,8 @@ import type { AuthContext } from "../types";
|
|
|
5
5
|
import { __getBetterAuthGlobal } from "./global";
|
|
6
6
|
|
|
7
7
|
export type AuthEndpointContext = Partial<
|
|
8
|
-
InputContext<string, any
|
|
8
|
+
InputContext<string, any, any, any, any, any> &
|
|
9
|
+
EndpointContext<string, any, any, any, any, any, any, AuthContext>
|
|
9
10
|
> & {
|
|
10
11
|
context: AuthContext;
|
|
11
12
|
};
|
|
@@ -496,6 +496,7 @@ export const createAdapterFactory =
|
|
|
496
496
|
value,
|
|
497
497
|
operator = "eq",
|
|
498
498
|
connector = "AND",
|
|
499
|
+
mode = "sensitive",
|
|
499
500
|
} = w;
|
|
500
501
|
if (operator === "in") {
|
|
501
502
|
if (!Array.isArray(value)) {
|
|
@@ -606,6 +607,7 @@ export const createAdapterFactory =
|
|
|
606
607
|
connector,
|
|
607
608
|
field: fieldName,
|
|
608
609
|
value: newValue,
|
|
610
|
+
mode,
|
|
609
611
|
} satisfies CleanedWhere;
|
|
610
612
|
}) as any;
|
|
611
613
|
};
|
package/src/db/adapter/index.ts
CHANGED
|
@@ -328,6 +328,14 @@ export type Where = {
|
|
|
328
328
|
* @default AND
|
|
329
329
|
*/
|
|
330
330
|
connector?: ("AND" | "OR") | undefined;
|
|
331
|
+
/**
|
|
332
|
+
* Case sensitivity for string comparisons.
|
|
333
|
+
* When "insensitive", string equality and pattern matching (contains, starts_with, ends_with)
|
|
334
|
+
* will be case-insensitive. Only applies to string values.
|
|
335
|
+
*
|
|
336
|
+
* @default "sensitive"
|
|
337
|
+
*/
|
|
338
|
+
mode?: "sensitive" | "insensitive" | undefined;
|
|
331
339
|
};
|
|
332
340
|
|
|
333
341
|
/**
|
|
@@ -1,10 +1,48 @@
|
|
|
1
|
+
import type { Span } from "@opentelemetry/api";
|
|
1
2
|
import { SpanStatusCode, trace } from "@opentelemetry/api";
|
|
3
|
+
import { ATTR_HTTP_RESPONSE_STATUS_CODE } from "./attributes";
|
|
2
4
|
|
|
3
5
|
const INSTRUMENTATION_SCOPE = "better-auth";
|
|
4
6
|
const INSTRUMENTATION_VERSION = import.meta.env?.BETTER_AUTH_VERSION ?? "1.0.0";
|
|
5
7
|
|
|
6
8
|
const tracer = trace.getTracer(INSTRUMENTATION_SCOPE, INSTRUMENTATION_VERSION);
|
|
7
9
|
|
|
10
|
+
/**
|
|
11
|
+
* Better-auth uses `throw ctx.redirect(url)` for flow control (e.g. OAuth
|
|
12
|
+
* callbacks). These are APIErrors with 3xx status codes and should not be
|
|
13
|
+
* recorded as span errors.
|
|
14
|
+
*/
|
|
15
|
+
function isRedirectError(err: unknown): boolean {
|
|
16
|
+
if (
|
|
17
|
+
err != null &&
|
|
18
|
+
typeof err === "object" &&
|
|
19
|
+
"name" in err &&
|
|
20
|
+
(err as { name: string }).name === "APIError" &&
|
|
21
|
+
"statusCode" in err
|
|
22
|
+
) {
|
|
23
|
+
const status = (err as { statusCode: number }).statusCode;
|
|
24
|
+
return status >= 300 && status < 400;
|
|
25
|
+
}
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function endSpanWithError(span: Span, err: unknown) {
|
|
30
|
+
if (isRedirectError(err)) {
|
|
31
|
+
span.setAttribute(
|
|
32
|
+
ATTR_HTTP_RESPONSE_STATUS_CODE,
|
|
33
|
+
(err as { statusCode: number }).statusCode,
|
|
34
|
+
);
|
|
35
|
+
span.setStatus({ code: SpanStatusCode.OK });
|
|
36
|
+
} else {
|
|
37
|
+
span.recordException(err as Error);
|
|
38
|
+
span.setStatus({
|
|
39
|
+
code: SpanStatusCode.ERROR,
|
|
40
|
+
message: String((err as Error)?.message ?? err),
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
span.end();
|
|
44
|
+
}
|
|
45
|
+
|
|
8
46
|
/**
|
|
9
47
|
* Creates a child span whose lifetime is bound to the execution of the given function
|
|
10
48
|
*
|
|
@@ -38,24 +76,14 @@ export function withSpan<T>(
|
|
|
38
76
|
return value;
|
|
39
77
|
})
|
|
40
78
|
.catch((err) => {
|
|
41
|
-
span
|
|
42
|
-
span.setStatus({
|
|
43
|
-
code: SpanStatusCode.ERROR,
|
|
44
|
-
message: String(err.message ?? err),
|
|
45
|
-
});
|
|
46
|
-
span.end();
|
|
79
|
+
endSpanWithError(span, err);
|
|
47
80
|
throw err;
|
|
48
81
|
}) as Promise<T>;
|
|
49
82
|
}
|
|
50
83
|
span.end();
|
|
51
84
|
return result;
|
|
52
85
|
} catch (err) {
|
|
53
|
-
span
|
|
54
|
-
span.setStatus({
|
|
55
|
-
code: SpanStatusCode.ERROR,
|
|
56
|
-
message: String((err as Error)?.message ?? err),
|
|
57
|
-
});
|
|
58
|
-
span.end();
|
|
86
|
+
endSpanWithError(span, err);
|
|
59
87
|
throw err;
|
|
60
88
|
}
|
|
61
89
|
});
|
|
@@ -106,11 +106,9 @@ export * from "./google";
|
|
|
106
106
|
export * from "./huggingface";
|
|
107
107
|
export * from "./kakao";
|
|
108
108
|
export * from "./kick";
|
|
109
|
-
export * from "./kick";
|
|
110
109
|
export * from "./line";
|
|
111
110
|
export * from "./linear";
|
|
112
111
|
export * from "./linkedin";
|
|
113
|
-
export * from "./linkedin";
|
|
114
112
|
export * from "./microsoft-entra-id";
|
|
115
113
|
export * from "./naver";
|
|
116
114
|
export * from "./notion";
|
package/src/types/context.ts
CHANGED
|
@@ -58,6 +58,7 @@ type InferPluginOptions<
|
|
|
58
58
|
*
|
|
59
59
|
* const createMyPlugin = <Options extends MyPluginOptions>(options?: Options) => ({
|
|
60
60
|
* id: 'my-plugin',
|
|
61
|
+
* version: '1.0.0',
|
|
61
62
|
* options,
|
|
62
63
|
* } satisfies BetterAuthPlugin);
|
|
63
64
|
*
|
|
@@ -79,9 +80,7 @@ export type BetterAuthPluginRegistryIdentifier = keyof BetterAuthPluginRegistry<
|
|
|
79
80
|
|
|
80
81
|
export type GenericEndpointContext<
|
|
81
82
|
Options extends BetterAuthOptions = BetterAuthOptions,
|
|
82
|
-
> = EndpointContext<string, any
|
|
83
|
-
context: AuthContext<Options>;
|
|
84
|
-
};
|
|
83
|
+
> = EndpointContext<string, any, any, any, any, any, any, AuthContext<Options>>;
|
|
85
84
|
|
|
86
85
|
export interface InternalAdapter<
|
|
87
86
|
_Options extends BetterAuthOptions = BetterAuthOptions,
|
|
@@ -216,7 +216,9 @@ export type BetterAuthAdvancedOptions = {
|
|
|
216
216
|
}
|
|
217
217
|
| undefined;
|
|
218
218
|
/**
|
|
219
|
-
*
|
|
219
|
+
* Force cookies to always use the `Secure` attribute. By default,
|
|
220
|
+
* cookies are secure in production environments. Set this to `true`
|
|
221
|
+
* to enforce secure cookies in all environments.
|
|
220
222
|
*
|
|
221
223
|
* @default false
|
|
222
224
|
*/
|
|
@@ -384,9 +386,11 @@ export type BetterAuthAdvancedOptions = {
|
|
|
384
386
|
|
|
385
387
|
export type BetterAuthOptions = {
|
|
386
388
|
/**
|
|
387
|
-
* The name of
|
|
389
|
+
* The name of your application. Used as a display name in contexts
|
|
390
|
+
* where your app needs to be identified — for example, as the default
|
|
391
|
+
* issuer name in authenticator apps when users set up 2FA/TOTP.
|
|
388
392
|
*
|
|
389
|
-
*
|
|
393
|
+
* Can also be set via the `APP_NAME` environment variable.
|
|
390
394
|
*
|
|
391
395
|
* @default "Better Auth"
|
|
392
396
|
*/
|
|
@@ -1145,7 +1149,12 @@ export type BetterAuthOptions = {
|
|
|
1145
1149
|
})
|
|
1146
1150
|
| undefined;
|
|
1147
1151
|
/**
|
|
1148
|
-
*
|
|
1152
|
+
* Additional trusted origins. By default, Better Auth trusts your
|
|
1153
|
+
* app's {@link baseURL}. Use this option to allow additional origins
|
|
1154
|
+
* (e.g. a separate frontend domain).
|
|
1155
|
+
*
|
|
1156
|
+
* Can be a static array, a function that returns origins dynamically,
|
|
1157
|
+
* or use wildcard patterns (e.g. `"https://*.example.com"`).
|
|
1149
1158
|
*
|
|
1150
1159
|
* @param request - The request object.
|
|
1151
1160
|
* It'll be undefined if no request was
|
package/src/types/plugin.ts
CHANGED
|
@@ -19,7 +19,20 @@ type DeepPartial<T> = T extends Function
|
|
|
19
19
|
: T;
|
|
20
20
|
|
|
21
21
|
export type HookEndpointContext = Partial<
|
|
22
|
-
EndpointContext<
|
|
22
|
+
EndpointContext<
|
|
23
|
+
string,
|
|
24
|
+
any,
|
|
25
|
+
any,
|
|
26
|
+
any,
|
|
27
|
+
any,
|
|
28
|
+
any,
|
|
29
|
+
any,
|
|
30
|
+
AuthContext & {
|
|
31
|
+
returned?: unknown | undefined;
|
|
32
|
+
responseHeaders?: Headers | undefined;
|
|
33
|
+
}
|
|
34
|
+
> &
|
|
35
|
+
Omit<InputContext<string, any, any, any, any, any>, "method">
|
|
23
36
|
> & {
|
|
24
37
|
path?: string;
|
|
25
38
|
context: AuthContext & {
|
|
@@ -38,6 +51,7 @@ export type BetterAuthPluginErrorCodePart = {
|
|
|
38
51
|
|
|
39
52
|
export type BetterAuthPlugin = BetterAuthPluginErrorCodePart & {
|
|
40
53
|
id: LiteralString;
|
|
54
|
+
version?: string | undefined;
|
|
41
55
|
/**
|
|
42
56
|
* The init function is called when the plugin is initialized.
|
|
43
57
|
* You can return a new context or modify the existing context.
|
package/dist/api/index.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"sources":["../../src/api/index.ts"],"sourcesContent":["import type {\n\tEndpointContext,\n\tEndpointOptions,\n\tStrictEndpoint,\n} from \"better-call\";\nimport { createEndpoint, createMiddleware } from \"better-call\";\nimport { runWithEndpointContext } from \"../context\";\nimport type { AuthContext } from \"../types\";\n\nexport const optionsMiddleware = createMiddleware(async () => {\n\t/**\n\t * This will be passed on the instance of\n\t * the context. Used to infer the type\n\t * here.\n\t */\n\treturn {} as AuthContext;\n});\n\nexport const createAuthMiddleware = createMiddleware.create({\n\tuse: [\n\t\toptionsMiddleware,\n\t\t/**\n\t\t * Only use for post hooks\n\t\t */\n\t\tcreateMiddleware(async () => {\n\t\t\treturn {} as {\n\t\t\t\treturned?: unknown | undefined;\n\t\t\t\tresponseHeaders?: Headers | undefined;\n\t\t\t};\n\t\t}),\n\t],\n});\n\nconst use = [optionsMiddleware];\n\ntype EndpointHandler<\n\tPath extends string,\n\tOptions extends EndpointOptions,\n\tR,\n> = (context: EndpointContext<Path, Options, AuthContext>) => Promise<R>;\n\nexport function createAuthEndpoint<\n\tPath extends string,\n\tOptions extends EndpointOptions,\n\tR,\n>(\n\tpath: Path,\n\toptions: Options,\n\thandler: EndpointHandler<Path, Options, R>,\n): StrictEndpoint<Path, Options, R>;\n\nexport function createAuthEndpoint<\n\tPath extends string,\n\tOptions extends EndpointOptions,\n\tR,\n>(\n\toptions: Options,\n\thandler: EndpointHandler<Path, Options, R>,\n): StrictEndpoint<Path, Options, R>;\n\nexport function createAuthEndpoint<\n\tPath extends string,\n\tOpts extends EndpointOptions,\n\tR,\n>(\n\tpathOrOptions: Path | Opts,\n\thandlerOrOptions: EndpointHandler<Path, Opts, R> | Opts,\n\thandlerOrNever?: any,\n) {\n\tconst path: Path | undefined =\n\t\ttypeof pathOrOptions === \"string\" ? pathOrOptions : undefined;\n\tconst options: Opts =\n\t\ttypeof handlerOrOptions === \"object\"\n\t\t\t? handlerOrOptions\n\t\t\t: (pathOrOptions as Opts);\n\tconst handler: EndpointHandler<Path, Opts, R> =\n\t\ttypeof handlerOrOptions === \"function\" ? handlerOrOptions : handlerOrNever;\n\n\tif (path) {\n\t\treturn createEndpoint(\n\t\t\tpath,\n\t\t\t{\n\t\t\t\t...options,\n\t\t\t\tuse: [...(options?.use || []), ...use],\n\t\t\t},\n\t\t\t// todo: prettify the code, we want to call `runWithEndpointContext` to top level\n\t\t\tasync (ctx) => runWithEndpointContext(ctx as any, () => handler(ctx)),\n\t\t);\n\t}\n\n\treturn createEndpoint(\n\t\t{\n\t\t\t...options,\n\t\t\tuse: [...(options?.use || []), ...use],\n\t\t},\n\t\t// todo: prettify the code, we want to call `runWithEndpointContext` to top level\n\t\tasync (ctx) => runWithEndpointContext(ctx as any, () => handler(ctx)),\n\t);\n}\n\nexport type AuthEndpoint<\n\tPath extends string,\n\tOpts extends EndpointOptions,\n\tR,\n> = ReturnType<typeof createAuthEndpoint<Path, Opts, R>>;\nexport type AuthMiddleware = ReturnType<typeof createAuthMiddleware>;\n"],"mappings":";;;;;AASA,MAAa,oBAAoB,iBAAiB,YAAY;;;;;;AAM7D,QAAO,EAAE;EACR;AAEF,MAAa,uBAAuB,iBAAiB,OAAO,EAC3D,KAAK,CACJ,mBAIA,iBAAiB,YAAY;AAC5B,QAAO,EAAE;EAIR,CACF,EACD,CAAC;AAEF,MAAM,MAAM,CAAC,kBAAkB;AA2B/B,SAAgB,mBAKf,eACA,kBACA,gBACC;CACD,MAAM,OACL,OAAO,kBAAkB,WAAW,gBAAgB;CACrD,MAAM,UACL,OAAO,qBAAqB,WACzB,mBACC;CACL,MAAM,UACL,OAAO,qBAAqB,aAAa,mBAAmB;AAE7D,KAAI,KACH,QAAO,eACN,MACA;EACC,GAAG;EACH,KAAK,CAAC,GAAI,SAAS,OAAO,EAAE,EAAG,GAAG,IAAI;EACtC,EAED,OAAO,QAAQ,uBAAuB,WAAkB,QAAQ,IAAI,CAAC,CACrE;AAGF,QAAO,eACN;EACC,GAAG;EACH,KAAK,CAAC,GAAI,SAAS,OAAO,EAAE,EAAG,GAAG,IAAI;EACtC,EAED,OAAO,QAAQ,uBAAuB,WAAkB,QAAQ,IAAI,CAAC,CACrE"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"sources":["../../src/async_hooks/index.ts"],"sourcesContent":["import type { AsyncLocalStorage } from \"node:async_hooks\";\n\nexport type { AsyncLocalStorage };\n\nconst AsyncLocalStoragePromise: Promise<typeof AsyncLocalStorage | null> =\n\timport(\n\t\t/* @vite-ignore */\n\t\t/* webpackIgnore: true */\n\t\t\"node:async_hooks\"\n\t)\n\t\t.then((mod) => mod.AsyncLocalStorage)\n\t\t.catch((err) => {\n\t\t\tif (\"AsyncLocalStorage\" in globalThis) {\n\t\t\t\treturn (globalThis as any).AsyncLocalStorage;\n\t\t\t}\n\t\t\tif (typeof window !== \"undefined\") {\n\t\t\t\treturn null;\n\t\t\t}\n\t\t\tconsole.warn(\n\t\t\t\t\"[better-auth] Warning: AsyncLocalStorage is not available in this environment. Some features may not work as expected.\",\n\t\t\t);\n\t\t\tconsole.warn(\n\t\t\t\t\"[better-auth] Please read more about this warning at https://better-auth.com/docs/installation#mount-handler\",\n\t\t\t);\n\t\t\tconsole.warn(\n\t\t\t\t\"[better-auth] If you are using Cloudflare Workers, please see: https://developers.cloudflare.com/workers/configuration/compatibility-flags/#nodejs-compatibility-flag\",\n\t\t\t);\n\t\t\tthrow err;\n\t\t});\n\nexport async function getAsyncLocalStorage(): Promise<\n\ttypeof AsyncLocalStorage\n> {\n\tconst mod = await AsyncLocalStoragePromise;\n\tif (mod === null) {\n\t\tthrow new Error(\"getAsyncLocalStorage is only available in server code\");\n\t} else {\n\t\treturn mod;\n\t}\n}\n"],"mappings":";AAIA,MAAM,2BACL;;;CAGC;EAEC,MAAM,QAAQ,IAAI,kBAAkB,CACpC,OAAO,QAAQ;AACf,KAAI,uBAAuB,WAC1B,QAAQ,WAAmB;AAE5B,KAAI,OAAO,WAAW,YACrB,QAAO;AAER,SAAQ,KACP,yHACA;AACD,SAAQ,KACP,+GACA;AACD,SAAQ,KACP,wKACA;AACD,OAAM;EACL;AAEJ,eAAsB,uBAEpB;CACD,MAAM,MAAM,MAAM;AAClB,KAAI,QAAQ,KACX,OAAM,IAAI,MAAM,wDAAwD;KAExE,QAAO"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"pure.index.mjs","names":["#current"],"sources":["../../src/async_hooks/pure.index.ts"],"sourcesContent":["import type { AsyncLocalStorage } from \"node:async_hooks\";\n\n/**\n * Due to the lack of AsyncLocalStorage in some environments (like Convex),\n *\n * We assume serverless functions are short-lived and single-threaded, so we can use a simple polyfill.\n */\nclass AsyncLocalStoragePolyfill<T> {\n\t#current: T | undefined = undefined;\n\n\trun(store: T, fn: () => unknown): unknown {\n\t\tconst prev = this.#current;\n\t\tthis.#current = store;\n\t\tconst result = fn();\n\t\tif (result instanceof Promise) {\n\t\t\treturn result.finally(() => {\n\t\t\t\tthis.#current = prev;\n\t\t\t});\n\t\t}\n\t\tthis.#current = prev;\n\t\treturn result;\n\t}\n\n\tgetStore(): T | undefined {\n\t\treturn this.#current;\n\t}\n}\n\nconst AsyncLocalStoragePromise: Promise<typeof AsyncLocalStorage | null> =\n\tPromise.resolve().then(() => {\n\t\tif (\"AsyncLocalStorage\" in globalThis) {\n\t\t\treturn (globalThis as any).AsyncLocalStorage;\n\t\t}\n\t\treturn AsyncLocalStoragePolyfill;\n\t});\n\nexport async function getAsyncLocalStorage(): Promise<\n\ttypeof AsyncLocalStorage\n> {\n\tconst mod = await AsyncLocalStoragePromise;\n\tif (mod === null) {\n\t\tthrow new Error(\"getAsyncLocalStorage is only available in server code\");\n\t} else {\n\t\treturn mod;\n\t}\n}\n"],"mappings":";;;;;;AAOA,IAAM,4BAAN,MAAmC;CAClC,WAA0B;CAE1B,IAAI,OAAU,IAA4B;EACzC,MAAM,OAAO,MAAKA;AAClB,QAAKA,UAAW;EAChB,MAAM,SAAS,IAAI;AACnB,MAAI,kBAAkB,QACrB,QAAO,OAAO,cAAc;AAC3B,SAAKA,UAAW;IACf;AAEH,QAAKA,UAAW;AAChB,SAAO;;CAGR,WAA0B;AACzB,SAAO,MAAKA;;;AAId,MAAM,2BACL,QAAQ,SAAS,CAAC,WAAW;AAC5B,KAAI,uBAAuB,WAC1B,QAAQ,WAAmB;AAE5B,QAAO;EACN;AAEH,eAAsB,uBAEpB;CACD,MAAM,MAAM,MAAM;AAClB,KAAI,QAAQ,KACX,OAAM,IAAI,MAAM,wDAAwD;KAExE,QAAO"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"endpoint-context.mjs","names":[],"sources":["../../src/context/endpoint-context.ts"],"sourcesContent":["import type { AsyncLocalStorage } from \"@better-auth/core/async_hooks\";\nimport { getAsyncLocalStorage } from \"@better-auth/core/async_hooks\";\nimport type { EndpointContext, InputContext } from \"better-call\";\nimport type { AuthContext } from \"../types\";\nimport { __getBetterAuthGlobal } from \"./global\";\n\nexport type AuthEndpointContext = Partial<\n\tInputContext<string, any> & EndpointContext<string, any>\n> & {\n\tcontext: AuthContext;\n};\n\nconst ensureAsyncStorage = async () => {\n\tconst betterAuthGlobal = __getBetterAuthGlobal();\n\tif (!betterAuthGlobal.context.endpointContextAsyncStorage) {\n\t\tconst AsyncLocalStorage = await getAsyncLocalStorage();\n\t\tbetterAuthGlobal.context.endpointContextAsyncStorage =\n\t\t\tnew AsyncLocalStorage<AuthEndpointContext>();\n\t}\n\treturn betterAuthGlobal.context\n\t\t.endpointContextAsyncStorage as AsyncLocalStorage<AuthEndpointContext>;\n};\n\n/**\n * This is for internal use only. Most users should use `getCurrentAuthContext` instead.\n *\n * It is exposed for advanced use cases where you need direct access to the AsyncLocalStorage instance.\n */\nexport async function getCurrentAuthContextAsyncLocalStorage() {\n\treturn ensureAsyncStorage();\n}\n\nexport async function getCurrentAuthContext(): Promise<AuthEndpointContext> {\n\tconst als = await ensureAsyncStorage();\n\tconst context = als.getStore();\n\tif (!context) {\n\t\tthrow new Error(\n\t\t\t\"No auth context found. Please make sure you are calling this function within a `runWithEndpointContext` callback.\",\n\t\t);\n\t}\n\treturn context;\n}\n\nexport async function runWithEndpointContext<T>(\n\tcontext: AuthEndpointContext,\n\tfn: () => T,\n): Promise<T> {\n\tconst als = await ensureAsyncStorage();\n\treturn als.run(context, fn);\n}\n"],"mappings":";;;;AAYA,MAAM,qBAAqB,YAAY;CACtC,MAAM,mBAAmB,uBAAuB;AAChD,KAAI,CAAC,iBAAiB,QAAQ,6BAA6B;EAC1D,MAAM,oBAAoB,MAAM,sBAAsB;AACtD,mBAAiB,QAAQ,8BACxB,IAAI,mBAAwC;;AAE9C,QAAO,iBAAiB,QACtB;;;;;;;AAQH,eAAsB,yCAAyC;AAC9D,QAAO,oBAAoB;;AAG5B,eAAsB,wBAAsD;CAE3E,MAAM,WADM,MAAM,oBAAoB,EAClB,UAAU;AAC9B,KAAI,CAAC,QACJ,OAAM,IAAI,MACT,oHACA;AAEF,QAAO;;AAGR,eAAsB,uBACrB,SACA,IACa;AAEb,SADY,MAAM,oBAAoB,EAC3B,IAAI,SAAS,GAAG"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"global.mjs","names":[],"sources":["../../src/context/global.ts"],"sourcesContent":["import type { AsyncLocalStorage } from \"@better-auth/core/async_hooks\";\n\ninterface BetterAuthGlobal {\n\t/**\n\t * The version of BetterAuth.\n\t */\n\tversion: string;\n\t/**\n\t * Used to track the number of BetterAuth instances in the same process.\n\t *\n\t * Debugging purposes only.\n\t */\n\tepoch: number;\n\t/**\n\t * Stores the AsyncLocalStorage instances for each context.\n\t */\n\tcontext: Record<string, AsyncLocalStorage<unknown>>;\n}\n\nconst symbol = Symbol.for(\"better-auth:global\");\nlet bind: BetterAuthGlobal | null = null;\n\nconst __context: Record<string, AsyncLocalStorage<unknown>> = {};\nconst __betterAuthVersion: string = import.meta.env\n\t.BETTER_AUTH_VERSION as string;\n\n/**\n * We store context instance in the globalThis.\n *\n * The reason we do this is that some bundlers, web framework, or package managers might\n * create multiple copies of BetterAuth in the same process intentionally or unintentionally.\n *\n * For example, yarn v1, Next.js, SSR, Vite...\n *\n * @internal\n */\nexport function __getBetterAuthGlobal(): BetterAuthGlobal {\n\tif (!(globalThis as any)[symbol]) {\n\t\t(globalThis as any)[symbol] = {\n\t\t\tversion: __betterAuthVersion,\n\t\t\tepoch: 1,\n\t\t\tcontext: __context,\n\t\t};\n\t\tbind = (globalThis as any)[symbol] as BetterAuthGlobal;\n\t}\n\tbind = (globalThis as any)[symbol] as BetterAuthGlobal;\n\tif (bind.version !== __betterAuthVersion) {\n\t\tbind.version = __betterAuthVersion;\n\t\t// Different versions of BetterAuth are loaded in the same process.\n\t\tbind.epoch++;\n\t}\n\treturn (globalThis as any)[symbol] as BetterAuthGlobal;\n}\n\nexport function getBetterAuthVersion(): string {\n\treturn __getBetterAuthGlobal().version;\n}\n"],"mappings":";AAmBA,MAAM,SAAS,OAAO,IAAI,qBAAqB;AAC/C,IAAI,OAAgC;AAEpC,MAAM,YAAwD,EAAE;AAChE,MAAM;;;;;;;;;;;AAaN,SAAgB,wBAA0C;AACzD,KAAI,CAAE,WAAmB,SAAS;AACjC,EAAC,WAAmB,UAAU;GAC7B,SAAS;GACT,OAAO;GACP,SAAS;GACT;AACD,SAAQ,WAAmB;;AAE5B,QAAQ,WAAmB;AAC3B,KAAI,KAAK,YAAY,qBAAqB;AACzC,OAAK,UAAU;AAEf,OAAK;;AAEN,QAAQ,WAAmB;;AAG5B,SAAgB,uBAA+B;AAC9C,QAAO,uBAAuB,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"request-state.mjs","names":[],"sources":["../../src/context/request-state.ts"],"sourcesContent":["import type { AsyncLocalStorage } from \"@better-auth/core/async_hooks\";\nimport { getAsyncLocalStorage } from \"@better-auth/core/async_hooks\";\nimport { __getBetterAuthGlobal } from \"./global\";\n\nexport type RequestStateWeakMap = WeakMap<object, any>;\n\nconst ensureAsyncStorage = async () => {\n\tconst betterAuthGlobal = __getBetterAuthGlobal();\n\tif (!betterAuthGlobal.context.requestStateAsyncStorage) {\n\t\tconst AsyncLocalStorage = await getAsyncLocalStorage();\n\t\tbetterAuthGlobal.context.requestStateAsyncStorage =\n\t\t\tnew AsyncLocalStorage<RequestStateWeakMap>();\n\t}\n\treturn betterAuthGlobal.context\n\t\t.requestStateAsyncStorage as AsyncLocalStorage<RequestStateWeakMap>;\n};\n\nexport async function getRequestStateAsyncLocalStorage() {\n\treturn ensureAsyncStorage();\n}\n\nexport async function hasRequestState() {\n\tconst als = await ensureAsyncStorage();\n\treturn als.getStore() !== undefined;\n}\n\nexport async function getCurrentRequestState(): Promise<RequestStateWeakMap> {\n\tconst als = await ensureAsyncStorage();\n\tconst store = als.getStore();\n\tif (!store) {\n\t\tthrow new Error(\n\t\t\t\"No request state found. Please make sure you are calling this function within a `runWithRequestState` callback.\",\n\t\t);\n\t}\n\treturn store;\n}\n\nexport async function runWithRequestState<T>(\n\tstore: RequestStateWeakMap,\n\tfn: () => T,\n): Promise<T> {\n\tconst als = await ensureAsyncStorage();\n\treturn als.run(store, fn);\n}\n\nexport interface RequestState<T> {\n\tget(): Promise<T>;\n\tset(value: T): Promise<void>;\n\n\t// A unique reference used as a key to identify this state within the request's WeakMap. Useful for debugging purposes.\n\treadonly ref: Readonly<object>;\n}\n\n/**\n * Defines a request-scoped state with lazy initialization.\n *\n * @param initFn - A function that initializes the state. It is called the first time `get()` is invoked within each request context, and only once per context.\n * @returns A RequestState object with `get` and `set` methods, and a unique `ref` for debugging.\n *\n * @example\n * const userState = defineRequestState(() => ({ id: '', name: '' }));\n * // Later, within a request context:\n * const user = await userState.get();\n */\nexport function defineRequestState<T>(\n\tinitFn: () => T | Promise<T>,\n): RequestState<T>;\nexport function defineRequestState(\n\tinitFn: () => any | Promise<any>,\n): RequestState<any> {\n\tconst ref = Object.freeze({});\n\treturn {\n\t\tget ref(): Readonly<object> {\n\t\t\treturn ref;\n\t\t},\n\t\tasync get() {\n\t\t\tconst store = await getCurrentRequestState();\n\t\t\tif (!store.has(ref)) {\n\t\t\t\tconst initialValue = await initFn();\n\t\t\t\tstore.set(ref, initialValue);\n\t\t\t\treturn initialValue;\n\t\t\t}\n\t\t\treturn store.get(ref);\n\t\t},\n\n\t\tasync set(value) {\n\t\t\tconst store = await getCurrentRequestState();\n\t\t\tstore.set(ref, value);\n\t\t},\n\t};\n}\n"],"mappings":";;;;AAMA,MAAM,qBAAqB,YAAY;CACtC,MAAM,mBAAmB,uBAAuB;AAChD,KAAI,CAAC,iBAAiB,QAAQ,0BAA0B;EACvD,MAAM,oBAAoB,MAAM,sBAAsB;AACtD,mBAAiB,QAAQ,2BACxB,IAAI,mBAAwC;;AAE9C,QAAO,iBAAiB,QACtB;;AAGH,eAAsB,mCAAmC;AACxD,QAAO,oBAAoB;;AAG5B,eAAsB,kBAAkB;AAEvC,SADY,MAAM,oBAAoB,EAC3B,UAAU,KAAK;;AAG3B,eAAsB,yBAAuD;CAE5E,MAAM,SADM,MAAM,oBAAoB,EACpB,UAAU;AAC5B,KAAI,CAAC,MACJ,OAAM,IAAI,MACT,kHACA;AAEF,QAAO;;AAGR,eAAsB,oBACrB,OACA,IACa;AAEb,SADY,MAAM,oBAAoB,EAC3B,IAAI,OAAO,GAAG;;AAyB1B,SAAgB,mBACf,QACoB;CACpB,MAAM,MAAM,OAAO,OAAO,EAAE,CAAC;AAC7B,QAAO;EACN,IAAI,MAAwB;AAC3B,UAAO;;EAER,MAAM,MAAM;GACX,MAAM,QAAQ,MAAM,wBAAwB;AAC5C,OAAI,CAAC,MAAM,IAAI,IAAI,EAAE;IACpB,MAAM,eAAe,MAAM,QAAQ;AACnC,UAAM,IAAI,KAAK,aAAa;AAC5B,WAAO;;AAER,UAAO,MAAM,IAAI,IAAI;;EAGtB,MAAM,IAAI,OAAO;AAEhB,IADc,MAAM,wBAAwB,EACtC,IAAI,KAAK,MAAM;;EAEtB"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"transaction.mjs","names":[],"sources":["../../src/context/transaction.ts"],"sourcesContent":["import type { AsyncLocalStorage } from \"node:async_hooks\";\nimport { getAsyncLocalStorage } from \"@better-auth/core/async_hooks\";\nimport type { DBAdapter, DBTransactionAdapter } from \"../db/adapter\";\nimport { __getBetterAuthGlobal } from \"./global\";\n\ntype HookContext = {\n\tadapter: DBTransactionAdapter;\n\tpendingHooks: Array<() => Promise<void>>;\n};\n\nconst ensureAsyncStorage = async () => {\n\tconst betterAuthGlobal = __getBetterAuthGlobal();\n\tif (!betterAuthGlobal.context.adapterAsyncStorage) {\n\t\tconst AsyncLocalStorage = await getAsyncLocalStorage();\n\t\tbetterAuthGlobal.context.adapterAsyncStorage = new AsyncLocalStorage();\n\t}\n\treturn betterAuthGlobal.context\n\t\t.adapterAsyncStorage as AsyncLocalStorage<HookContext>;\n};\n\n/**\n * This is for internal use only. Most users should use `getCurrentAdapter` instead.\n *\n * It is exposed for advanced use cases where you need direct access to the AsyncLocalStorage instance.\n */\nexport const getCurrentDBAdapterAsyncLocalStorage = async () => {\n\treturn ensureAsyncStorage();\n};\n\nexport const getCurrentAdapter = async (\n\tfallback: DBTransactionAdapter,\n): Promise<DBTransactionAdapter> => {\n\treturn ensureAsyncStorage()\n\t\t.then((als) => {\n\t\t\tconst store = als.getStore();\n\t\t\treturn store?.adapter || fallback;\n\t\t})\n\t\t.catch(() => {\n\t\t\treturn fallback;\n\t\t});\n};\n\nexport const runWithAdapter = async <R>(\n\tadapter: DBAdapter,\n\tfn: () => R,\n): Promise<R> => {\n\tlet called = false;\n\treturn ensureAsyncStorage()\n\t\t.then(async (als) => {\n\t\t\tcalled = true;\n\t\t\tconst pendingHooks: Array<() => Promise<void>> = [];\n\t\t\tlet result: Awaited<R>;\n\t\t\tlet error: unknown;\n\t\t\tlet hasError = false;\n\t\t\ttry {\n\t\t\t\tresult = await als.run({ adapter, pendingHooks }, fn);\n\t\t\t} catch (err) {\n\t\t\t\terror = err;\n\t\t\t\thasError = true;\n\t\t\t}\n\t\t\t// Execute pending hooks after the function completes (even if it threw)\n\t\t\tfor (const hook of pendingHooks) {\n\t\t\t\tawait hook();\n\t\t\t}\n\t\t\tif (hasError) {\n\t\t\t\tthrow error;\n\t\t\t}\n\t\t\treturn result!;\n\t\t})\n\t\t.catch((err) => {\n\t\t\tif (!called) {\n\t\t\t\treturn fn();\n\t\t\t}\n\t\t\tthrow err;\n\t\t});\n};\n\nexport const runWithTransaction = async <R>(\n\tadapter: DBAdapter,\n\tfn: () => R,\n): Promise<R> => {\n\tlet called = true;\n\treturn ensureAsyncStorage()\n\t\t.then(async (als) => {\n\t\t\tcalled = true;\n\t\t\tconst pendingHooks: Array<() => Promise<void>> = [];\n\t\t\tlet result: Awaited<R>;\n\t\t\tlet error: unknown;\n\t\t\tlet hasError = false;\n\t\t\ttry {\n\t\t\t\tresult = await adapter.transaction(async (trx) => {\n\t\t\t\t\treturn als.run({ adapter: trx, pendingHooks }, fn);\n\t\t\t\t});\n\t\t\t} catch (e) {\n\t\t\t\thasError = true;\n\t\t\t\terror = e;\n\t\t\t}\n\t\t\tfor (const hook of pendingHooks) {\n\t\t\t\tawait hook();\n\t\t\t}\n\t\t\tif (hasError) {\n\t\t\t\tthrow error;\n\t\t\t}\n\t\t\treturn result!;\n\t\t})\n\t\t.catch((err) => {\n\t\t\tif (!called) {\n\t\t\t\treturn fn();\n\t\t\t}\n\t\t\tthrow err;\n\t\t});\n};\n\n/**\n * Queue a hook to be executed after the current transaction commits.\n * If not in a transaction, the hook will execute immediately.\n */\nexport const queueAfterTransactionHook = async (\n\thook: () => Promise<void>,\n): Promise<void> => {\n\treturn ensureAsyncStorage()\n\t\t.then((als) => {\n\t\t\tconst store = als.getStore();\n\t\t\tif (store) {\n\t\t\t\t// We're in a transaction context, queue the hook\n\t\t\t\tstore.pendingHooks.push(hook);\n\t\t\t} else {\n\t\t\t\t// Not in a transaction, execute immediately\n\t\t\t\treturn hook();\n\t\t\t}\n\t\t})\n\t\t.catch(() => {\n\t\t\t// No async storage available, execute immediately\n\t\t\treturn hook();\n\t\t});\n};\n"],"mappings":";;;;AAUA,MAAM,qBAAqB,YAAY;CACtC,MAAM,mBAAmB,uBAAuB;AAChD,KAAI,CAAC,iBAAiB,QAAQ,qBAAqB;EAClD,MAAM,oBAAoB,MAAM,sBAAsB;AACtD,mBAAiB,QAAQ,sBAAsB,IAAI,mBAAmB;;AAEvE,QAAO,iBAAiB,QACtB;;;;;;;AAQH,MAAa,uCAAuC,YAAY;AAC/D,QAAO,oBAAoB;;AAG5B,MAAa,oBAAoB,OAChC,aACmC;AACnC,QAAO,oBAAoB,CACzB,MAAM,QAAQ;AAEd,SADc,IAAI,UAAU,EACd,WAAW;GACxB,CACD,YAAY;AACZ,SAAO;GACN;;AAGJ,MAAa,iBAAiB,OAC7B,SACA,OACgB;CAChB,IAAI,SAAS;AACb,QAAO,oBAAoB,CACzB,KAAK,OAAO,QAAQ;AACpB,WAAS;EACT,MAAM,eAA2C,EAAE;EACnD,IAAI;EACJ,IAAI;EACJ,IAAI,WAAW;AACf,MAAI;AACH,YAAS,MAAM,IAAI,IAAI;IAAE;IAAS;IAAc,EAAE,GAAG;WAC7C,KAAK;AACb,WAAQ;AACR,cAAW;;AAGZ,OAAK,MAAM,QAAQ,aAClB,OAAM,MAAM;AAEb,MAAI,SACH,OAAM;AAEP,SAAO;GACN,CACD,OAAO,QAAQ;AACf,MAAI,CAAC,OACJ,QAAO,IAAI;AAEZ,QAAM;GACL;;AAGJ,MAAa,qBAAqB,OACjC,SACA,OACgB;CAChB,IAAI,SAAS;AACb,QAAO,oBAAoB,CACzB,KAAK,OAAO,QAAQ;AACpB,WAAS;EACT,MAAM,eAA2C,EAAE;EACnD,IAAI;EACJ,IAAI;EACJ,IAAI,WAAW;AACf,MAAI;AACH,YAAS,MAAM,QAAQ,YAAY,OAAO,QAAQ;AACjD,WAAO,IAAI,IAAI;KAAE,SAAS;KAAK;KAAc,EAAE,GAAG;KACjD;WACM,GAAG;AACX,cAAW;AACX,WAAQ;;AAET,OAAK,MAAM,QAAQ,aAClB,OAAM,MAAM;AAEb,MAAI,SACH,OAAM;AAEP,SAAO;GACN,CACD,OAAO,QAAQ;AACf,MAAI,CAAC,OACJ,QAAO,IAAI;AAEZ,QAAM;GACL;;;;;;AAOJ,MAAa,4BAA4B,OACxC,SACmB;AACnB,QAAO,oBAAoB,CACzB,MAAM,QAAQ;EACd,MAAM,QAAQ,IAAI,UAAU;AAC5B,MAAI,MAEH,OAAM,aAAa,KAAK,KAAK;MAG7B,QAAO,MAAM;GAEb,CACD,YAAY;AAEZ,SAAO,MAAM;GACZ"}
|