@better-auth/core 1.4.12-beta.2 → 1.4.13

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.
Files changed (185) hide show
  1. package/.turbo/turbo-build.log +172 -35
  2. package/dist/api/index.d.mts +178 -1
  3. package/dist/api/index.mjs +2 -1
  4. package/dist/context/endpoint-context.d.mts +19 -0
  5. package/dist/context/endpoint-context.mjs +31 -0
  6. package/dist/context/global.d.mts +7 -0
  7. package/dist/context/global.mjs +37 -0
  8. package/dist/context/index.d.mts +5 -53
  9. package/dist/context/index.mjs +5 -2
  10. package/dist/context/request-state.d.mts +27 -0
  11. package/dist/context/request-state.mjs +49 -0
  12. package/dist/context/transaction.d.mts +16 -0
  13. package/dist/context/transaction.mjs +52 -0
  14. package/dist/db/adapter/factory.d.mts +27 -0
  15. package/dist/db/adapter/factory.mjs +738 -0
  16. package/dist/db/adapter/get-default-field-name.d.mts +18 -0
  17. package/dist/db/adapter/get-default-field-name.mjs +38 -0
  18. package/dist/db/adapter/get-default-model-name.d.mts +12 -0
  19. package/dist/db/adapter/get-default-model-name.mjs +32 -0
  20. package/dist/db/adapter/get-field-attributes.d.mts +29 -0
  21. package/dist/db/adapter/get-field-attributes.mjs +39 -0
  22. package/dist/db/adapter/get-field-name.d.mts +18 -0
  23. package/dist/db/adapter/get-field-name.mjs +33 -0
  24. package/dist/db/adapter/get-id-field.d.mts +39 -0
  25. package/dist/db/adapter/get-id-field.mjs +68 -0
  26. package/dist/db/adapter/get-model-name.d.mts +12 -0
  27. package/dist/db/adapter/get-model-name.mjs +23 -0
  28. package/dist/db/adapter/index.d.mts +513 -1
  29. package/dist/db/adapter/index.mjs +8 -970
  30. package/dist/db/adapter/types.d.mts +139 -0
  31. package/dist/db/adapter/utils.d.mts +7 -0
  32. package/dist/db/adapter/utils.mjs +38 -0
  33. package/dist/db/get-tables.d.mts +8 -0
  34. package/dist/{get-tables-CMc_Emww.mjs → db/get-tables.mjs} +1 -1
  35. package/dist/db/index.d.mts +10 -2
  36. package/dist/db/index.mjs +7 -60
  37. package/dist/db/plugin.d.mts +12 -0
  38. package/dist/db/schema/account.d.mts +26 -0
  39. package/dist/db/schema/account.mjs +19 -0
  40. package/dist/db/schema/rate-limit.d.mts +14 -0
  41. package/dist/db/schema/rate-limit.mjs +11 -0
  42. package/dist/db/schema/session.d.mts +21 -0
  43. package/dist/db/schema/session.mjs +14 -0
  44. package/dist/db/schema/shared.d.mts +10 -0
  45. package/dist/db/schema/shared.mjs +11 -0
  46. package/dist/db/schema/user.d.mts +20 -0
  47. package/dist/db/schema/user.mjs +13 -0
  48. package/dist/db/schema/verification.d.mts +19 -0
  49. package/dist/db/schema/verification.mjs +12 -0
  50. package/dist/db/type.d.mts +143 -0
  51. package/dist/env/color-depth.d.mts +4 -0
  52. package/dist/env/color-depth.mjs +88 -0
  53. package/dist/env/env-impl.d.mts +32 -0
  54. package/dist/env/env-impl.mjs +82 -0
  55. package/dist/env/index.d.mts +4 -2
  56. package/dist/env/index.mjs +3 -1
  57. package/dist/{index-BRBu0-5h.d.mts → env/logger.d.mts} +1 -35
  58. package/dist/env/logger.mjs +81 -0
  59. package/dist/error/codes.d.mts +48 -0
  60. package/dist/{error-DP1xOn7P.mjs → error/codes.mjs} +3 -14
  61. package/dist/error/index.d.mts +5 -48
  62. package/dist/error/index.mjs +12 -3
  63. package/dist/index.d.mts +8 -2
  64. package/dist/oauth2/client-credentials-token.d.mts +36 -0
  65. package/dist/oauth2/client-credentials-token.mjs +54 -0
  66. package/dist/oauth2/create-authorization-url.d.mts +45 -0
  67. package/dist/oauth2/create-authorization-url.mjs +42 -0
  68. package/dist/oauth2/index.d.mts +8 -2
  69. package/dist/oauth2/index.mjs +6 -2
  70. package/dist/oauth2/oauth-provider.d.mts +194 -0
  71. package/dist/oauth2/refresh-access-token.d.mts +36 -0
  72. package/dist/oauth2/refresh-access-token.mjs +58 -0
  73. package/dist/oauth2/utils.d.mts +7 -0
  74. package/dist/oauth2/utils.mjs +27 -0
  75. package/dist/oauth2/validate-authorization-code.d.mts +55 -0
  76. package/dist/oauth2/validate-authorization-code.mjs +71 -0
  77. package/dist/oauth2/verify.d.mts +49 -0
  78. package/dist/oauth2/verify.mjs +95 -0
  79. package/dist/social-providers/apple.d.mts +119 -0
  80. package/dist/social-providers/apple.mjs +102 -0
  81. package/dist/social-providers/atlassian.d.mts +72 -0
  82. package/dist/social-providers/atlassian.mjs +83 -0
  83. package/dist/social-providers/cognito.d.mts +87 -0
  84. package/dist/social-providers/cognito.mjs +166 -0
  85. package/dist/social-providers/discord.d.mts +126 -0
  86. package/dist/social-providers/discord.mjs +64 -0
  87. package/dist/social-providers/dropbox.d.mts +71 -0
  88. package/dist/social-providers/dropbox.mjs +75 -0
  89. package/dist/social-providers/facebook.d.mts +81 -0
  90. package/dist/social-providers/facebook.mjs +120 -0
  91. package/dist/social-providers/figma.d.mts +63 -0
  92. package/dist/social-providers/figma.mjs +84 -0
  93. package/dist/social-providers/github.d.mts +104 -0
  94. package/dist/social-providers/github.mjs +80 -0
  95. package/dist/social-providers/gitlab.d.mts +125 -0
  96. package/dist/social-providers/gitlab.mjs +82 -0
  97. package/dist/social-providers/google.d.mts +99 -0
  98. package/dist/social-providers/google.mjs +109 -0
  99. package/dist/social-providers/huggingface.d.mts +85 -0
  100. package/dist/social-providers/huggingface.mjs +75 -0
  101. package/dist/social-providers/index.d.mts +1723 -1
  102. package/dist/social-providers/index.mjs +33 -2570
  103. package/dist/social-providers/kakao.d.mts +163 -0
  104. package/dist/social-providers/kakao.mjs +72 -0
  105. package/dist/social-providers/kick.d.mts +75 -0
  106. package/dist/social-providers/kick.mjs +71 -0
  107. package/dist/social-providers/line.d.mts +107 -0
  108. package/dist/social-providers/line.mjs +113 -0
  109. package/dist/social-providers/linear.d.mts +70 -0
  110. package/dist/social-providers/linear.mjs +88 -0
  111. package/dist/social-providers/linkedin.d.mts +69 -0
  112. package/dist/social-providers/linkedin.mjs +76 -0
  113. package/dist/social-providers/microsoft-entra-id.d.mts +174 -0
  114. package/dist/social-providers/microsoft-entra-id.mjs +106 -0
  115. package/dist/social-providers/naver.d.mts +104 -0
  116. package/dist/social-providers/naver.mjs +67 -0
  117. package/dist/social-providers/notion.d.mts +66 -0
  118. package/dist/social-providers/notion.mjs +75 -0
  119. package/dist/social-providers/paybin.d.mts +73 -0
  120. package/dist/social-providers/paybin.mjs +85 -0
  121. package/dist/social-providers/paypal.d.mts +131 -0
  122. package/dist/social-providers/paypal.mjs +144 -0
  123. package/dist/social-providers/polar.d.mts +76 -0
  124. package/dist/social-providers/polar.mjs +73 -0
  125. package/dist/social-providers/reddit.d.mts +64 -0
  126. package/dist/social-providers/reddit.mjs +83 -0
  127. package/dist/social-providers/roblox.d.mts +72 -0
  128. package/dist/social-providers/roblox.mjs +59 -0
  129. package/dist/social-providers/salesforce.d.mts +81 -0
  130. package/dist/social-providers/salesforce.mjs +91 -0
  131. package/dist/social-providers/slack.d.mts +85 -0
  132. package/dist/social-providers/slack.mjs +68 -0
  133. package/dist/social-providers/spotify.d.mts +65 -0
  134. package/dist/social-providers/spotify.mjs +71 -0
  135. package/dist/social-providers/tiktok.d.mts +171 -0
  136. package/dist/social-providers/tiktok.mjs +62 -0
  137. package/dist/social-providers/twitch.d.mts +81 -0
  138. package/dist/social-providers/twitch.mjs +78 -0
  139. package/dist/social-providers/twitter.d.mts +140 -0
  140. package/dist/social-providers/twitter.mjs +87 -0
  141. package/dist/social-providers/vercel.d.mts +64 -0
  142. package/dist/social-providers/vercel.mjs +61 -0
  143. package/dist/social-providers/vk.d.mts +72 -0
  144. package/dist/social-providers/vk.mjs +83 -0
  145. package/dist/social-providers/zoom.d.mts +173 -0
  146. package/dist/social-providers/zoom.mjs +72 -0
  147. package/dist/types/context.d.mts +215 -0
  148. package/dist/types/cookie.d.mts +15 -0
  149. package/dist/types/helper.d.mts +8 -0
  150. package/dist/types/index.d.mts +8 -0
  151. package/dist/types/init-options.d.mts +1266 -0
  152. package/dist/types/plugin-client.d.mts +103 -0
  153. package/dist/types/plugin.d.mts +121 -0
  154. package/dist/utils/deprecate.d.mts +10 -0
  155. package/dist/utils/deprecate.mjs +17 -0
  156. package/dist/utils/error-codes.d.mts +9 -0
  157. package/dist/utils/error-codes.mjs +7 -0
  158. package/dist/utils/id.d.mts +4 -0
  159. package/dist/utils/id.mjs +9 -0
  160. package/dist/utils/index.d.mts +5 -26
  161. package/dist/utils/index.mjs +5 -2
  162. package/dist/utils/json.d.mts +4 -0
  163. package/dist/utils/json.mjs +25 -0
  164. package/dist/utils/string.d.mts +4 -0
  165. package/dist/utils/string.mjs +7 -0
  166. package/package.json +1 -1
  167. package/src/context/endpoint-context.ts +7 -15
  168. package/src/context/global.ts +57 -0
  169. package/src/context/index.ts +1 -0
  170. package/src/context/request-state.ts +7 -12
  171. package/src/context/transaction.ts +7 -16
  172. package/src/db/adapter/factory.ts +13 -13
  173. package/src/db/adapter/get-default-model-name.ts +1 -1
  174. package/src/db/adapter/get-id-field.ts +2 -2
  175. package/src/error/index.ts +2 -3
  176. package/src/social-providers/gitlab.ts +1 -1
  177. package/src/types/context.ts +137 -131
  178. package/src/types/cookie.ts +6 -4
  179. package/src/types/index.ts +2 -1
  180. package/tsdown.config.ts +9 -0
  181. package/dist/context-BGZ8V6DD.mjs +0 -126
  182. package/dist/env-DbssmzoK.mjs +0 -245
  183. package/dist/index-zgYuzZ7O.d.mts +0 -8020
  184. package/dist/oauth2-COJkghlT.mjs +0 -326
  185. package/dist/utils-U2L7n92V.mjs +0 -59
@@ -0,0 +1,103 @@
1
+ import { LiteralString } from "./helper.mjs";
2
+ import { BetterAuthPlugin } from "./plugin.mjs";
3
+ import { BetterAuthOptions } from "./init-options.mjs";
4
+ import { BetterFetch, BetterFetchOption, BetterFetchPlugin } from "@better-fetch/fetch";
5
+ import { Atom, WritableAtom } from "nanostores";
6
+
7
+ //#region src/types/plugin-client.d.ts
8
+ interface ClientStore {
9
+ notify: (signal: string) => void;
10
+ listen: (signal: string, listener: () => void) => void;
11
+ atoms: Record<string, WritableAtom<any>>;
12
+ }
13
+ type ClientAtomListener = {
14
+ matcher: (path: string) => boolean;
15
+ signal: "$sessionSignal" | Omit<string, "$sessionSignal">;
16
+ };
17
+ /**
18
+ * Better-Fetch options but with additional options for the auth-client.
19
+ */
20
+ type ClientFetchOption<Body = any, Query extends Record<string, any> = any, Params extends Record<string, any> | Array<string> | undefined = any, Res = any> = BetterFetchOption<Body, Query, Params, Res> & {
21
+ /**
22
+ * Certain endpoints, upon successful response, will trigger atom signals and thus rerendering all hooks related to that atom.
23
+ *
24
+ * This option is useful when you want to skip hook rerenders.
25
+ */
26
+ disableSignal?: boolean | undefined;
27
+ };
28
+ interface RevalidateOptions {
29
+ /**
30
+ * A time interval (in seconds) after which the session will be re-fetched.
31
+ * If set to `0` (default), the session is not polled.
32
+ *
33
+ * This helps prevent session expiry during idle periods by periodically
34
+ * refreshing the session.
35
+ *
36
+ * @default 0
37
+ */
38
+ refetchInterval?: number | undefined;
39
+ /**
40
+ * Automatically refetch the session when the user switches back to the window/tab.
41
+ * This option activates this behavior if set to `true` (default).
42
+ *
43
+ * Prevents expired sessions when users switch tabs and come back later.
44
+ *
45
+ * @default true
46
+ */
47
+ refetchOnWindowFocus?: boolean | undefined;
48
+ /**
49
+ * Set to `false` to stop polling when the device has no internet access
50
+ * (determined by `navigator.onLine`).
51
+ *
52
+ * @default false
53
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/NavigatorOnLine/onLine
54
+ */
55
+ refetchWhenOffline?: boolean | undefined;
56
+ }
57
+ interface BetterAuthClientOptions {
58
+ fetchOptions?: ClientFetchOption | undefined;
59
+ plugins?: BetterAuthClientPlugin[] | undefined;
60
+ baseURL?: string | undefined;
61
+ basePath?: string | undefined;
62
+ disableDefaultFetchPlugins?: boolean | undefined;
63
+ $InferAuth?: BetterAuthOptions | undefined;
64
+ sessionOptions?: RevalidateOptions | undefined;
65
+ }
66
+ interface BetterAuthClientPlugin {
67
+ id: LiteralString;
68
+ /**
69
+ * only used for type inference. don't pass the
70
+ * actual plugin
71
+ */
72
+ $InferServerPlugin?: BetterAuthPlugin | undefined;
73
+ /**
74
+ * Custom actions
75
+ */
76
+ getActions?: ($fetch: BetterFetch, $store: ClientStore,
77
+ /**
78
+ * better-auth client options
79
+ */
80
+ options: BetterAuthClientOptions | undefined) => Record<string, any>;
81
+ /**
82
+ * State atoms that'll be resolved by each framework
83
+ * auth store.
84
+ */
85
+ getAtoms?: (($fetch: BetterFetch) => Record<string, Atom<any>>) | undefined;
86
+ /**
87
+ * specify path methods for server plugin inferred
88
+ * endpoints to force a specific method.
89
+ */
90
+ pathMethods?: Record<string, "POST" | "GET"> | undefined;
91
+ /**
92
+ * Better fetch plugins
93
+ */
94
+ fetchPlugins?: BetterFetchPlugin[] | undefined;
95
+ /**
96
+ * a list of recaller based on a matcher function.
97
+ * The signal name needs to match a signal in this
98
+ * plugin or any plugin the user might have added.
99
+ */
100
+ atomListeners?: ClientAtomListener[] | undefined;
101
+ }
102
+ //#endregion
103
+ export { BetterAuthClientOptions, BetterAuthClientPlugin, ClientAtomListener, ClientFetchOption, ClientStore };
@@ -0,0 +1,121 @@
1
+ import { BetterAuthPluginDBSchema } from "../db/plugin.mjs";
2
+ import "../db/index.mjs";
3
+ import { Awaitable, LiteralString } from "./helper.mjs";
4
+ import { BetterAuthOptions } from "./init-options.mjs";
5
+ import { AuthContext } from "./context.mjs";
6
+ import { AuthMiddleware } from "../api/index.mjs";
7
+ import { Endpoint, EndpointContext, InputContext, Middleware } from "better-call";
8
+ import { Migration } from "kysely";
9
+
10
+ //#region src/types/plugin.d.ts
11
+ type DeepPartial<T> = T extends Function ? T : T extends object ? { [K in keyof T]?: DeepPartial<T[K]> } : T;
12
+ type HookEndpointContext = Partial<EndpointContext<string, any> & Omit<InputContext<string, any>, "method">> & {
13
+ path?: string;
14
+ context: AuthContext & {
15
+ returned?: unknown | undefined;
16
+ responseHeaders?: Headers | undefined;
17
+ };
18
+ headers?: Headers | undefined;
19
+ };
20
+ type BetterAuthPlugin = {
21
+ id: LiteralString;
22
+ /**
23
+ * The init function is called when the plugin is initialized.
24
+ * You can return a new context or modify the existing context.
25
+ */
26
+ init?: ((ctx: AuthContext) => Awaitable<{
27
+ context?: DeepPartial<Omit<AuthContext, "options">>;
28
+ options?: Partial<BetterAuthOptions>;
29
+ }> | void | Promise<void>) | undefined;
30
+ endpoints?: {
31
+ [key: string]: Endpoint;
32
+ } | undefined;
33
+ middlewares?: {
34
+ path: string;
35
+ middleware: Middleware;
36
+ }[] | undefined;
37
+ onRequest?: ((request: Request, ctx: AuthContext) => Promise<{
38
+ response: Response;
39
+ } | {
40
+ request: Request;
41
+ } | void>) | undefined;
42
+ onResponse?: ((response: Response, ctx: AuthContext) => Promise<{
43
+ response: Response;
44
+ } | void>) | undefined;
45
+ hooks?: {
46
+ before?: {
47
+ matcher: (context: HookEndpointContext) => boolean;
48
+ handler: AuthMiddleware;
49
+ }[];
50
+ after?: {
51
+ matcher: (context: HookEndpointContext) => boolean;
52
+ handler: AuthMiddleware;
53
+ }[];
54
+ } | undefined;
55
+ /**
56
+ * Schema the plugin needs
57
+ *
58
+ * This will also be used to migrate the database. If the fields are dynamic from the plugins
59
+ * configuration each time the configuration is changed a new migration will be created.
60
+ *
61
+ * NOTE: If you want to create migrations manually using
62
+ * migrations option or any other way you
63
+ * can disable migration per table basis.
64
+ *
65
+ * @example
66
+ * ```ts
67
+ * schema: {
68
+ * user: {
69
+ * fields: {
70
+ * email: {
71
+ * type: "string",
72
+ * },
73
+ * emailVerified: {
74
+ * type: "boolean",
75
+ * defaultValue: false,
76
+ * },
77
+ * },
78
+ * }
79
+ * } as AuthPluginSchema
80
+ * ```
81
+ */
82
+ schema?: BetterAuthPluginDBSchema | undefined;
83
+ /**
84
+ * The migrations of the plugin. If you define schema that will automatically create
85
+ * migrations for you.
86
+ *
87
+ * ⚠️ Only uses this if you dont't want to use the schema option and you disabled migrations for
88
+ * the tables.
89
+ */
90
+ migrations?: Record<string, Migration> | undefined;
91
+ /**
92
+ * The options of the plugin
93
+ */
94
+ options?: Record<string, any> | undefined;
95
+ /**
96
+ * types to be inferred
97
+ */
98
+ $Infer?: Record<string, any> | undefined;
99
+ /**
100
+ * The rate limit rules to apply to specific paths.
101
+ */
102
+ rateLimit?: {
103
+ window: number;
104
+ max: number;
105
+ pathMatcher: (path: string) => boolean;
106
+ }[] | undefined;
107
+ /**
108
+ * The error codes returned by the plugin
109
+ */
110
+ $ERROR_CODES?: Record<string, string> | undefined;
111
+ /**
112
+ * All database operations that are performed by the plugin
113
+ *
114
+ * This will override the default database operations
115
+ */
116
+ adapter?: {
117
+ [key: string]: (...args: any[]) => Awaitable<any>;
118
+ };
119
+ };
120
+ //#endregion
121
+ export { BetterAuthPlugin, HookEndpointContext };
@@ -0,0 +1,10 @@
1
+ import { InternalLogger } from "../env/logger.mjs";
2
+
3
+ //#region src/utils/deprecate.d.ts
4
+
5
+ /**
6
+ * Wraps a function to log a deprecation warning at once.
7
+ */
8
+ declare function deprecate<T extends (...args: any[]) => any>(fn: T, message: string, logger?: InternalLogger): T;
9
+ //#endregion
10
+ export { deprecate };
@@ -0,0 +1,17 @@
1
+ //#region src/utils/deprecate.ts
2
+ /**
3
+ * Wraps a function to log a deprecation warning at once.
4
+ */
5
+ function deprecate(fn, message, logger) {
6
+ let warned = false;
7
+ return function(...args) {
8
+ if (!warned) {
9
+ (logger?.warn ?? console.warn)(`[Deprecation] ${message}`);
10
+ warned = true;
11
+ }
12
+ return fn.apply(this, args);
13
+ };
14
+ }
15
+
16
+ //#endregion
17
+ export { deprecate };
@@ -0,0 +1,9 @@
1
+ //#region src/utils/error-codes.d.ts
2
+ type UpperLetter = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z";
3
+ type SpecialCharacter = "_";
4
+ type IsValidUpperSnakeCase<S extends string> = S extends `${infer F}${infer R}` ? F extends UpperLetter | SpecialCharacter ? IsValidUpperSnakeCase<R> : false : true;
5
+ type InvalidKeyError<K$1 extends string> = `Invalid error code key: "${K$1}" - must only contain uppercase letters (A-Z) and underscores (_)`;
6
+ type ValidateErrorCodes<T> = { [K in keyof T]: K extends string ? IsValidUpperSnakeCase<K> extends false ? InvalidKeyError<K> : T[K] : T[K] };
7
+ declare function defineErrorCodes<const T extends Record<string, string>>(codes: ValidateErrorCodes<T>): T;
8
+ //#endregion
9
+ export { defineErrorCodes };
@@ -0,0 +1,7 @@
1
+ //#region src/utils/error-codes.ts
2
+ function defineErrorCodes(codes) {
3
+ return codes;
4
+ }
5
+
6
+ //#endregion
7
+ export { defineErrorCodes };
@@ -0,0 +1,4 @@
1
+ //#region src/utils/id.d.ts
2
+ declare const generateId: (size?: number) => string;
3
+ //#endregion
4
+ export { generateId };
@@ -0,0 +1,9 @@
1
+ import { createRandomStringGenerator } from "@better-auth/utils/random";
2
+
3
+ //#region src/utils/id.ts
4
+ const generateId = (size) => {
5
+ return createRandomStringGenerator("a-z", "A-Z", "0-9")(size || 32);
6
+ };
7
+
8
+ //#endregion
9
+ export { generateId };
@@ -1,27 +1,6 @@
1
- import { t as InternalLogger } from "../index-BRBu0-5h.mjs";
2
-
3
- //#region src/utils/deprecate.d.ts
4
-
5
- /**
6
- * Wraps a function to log a deprecation warning at once.
7
- */
8
- declare function deprecate<T extends (...args: any[]) => any>(fn: T, message: string, logger?: InternalLogger): T;
9
- //#endregion
10
- //#region src/utils/error-codes.d.ts
11
- type UpperLetter = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z";
12
- type SpecialCharacter = "_";
13
- type IsValidUpperSnakeCase<S extends string> = S extends `${infer F}${infer R}` ? F extends UpperLetter | SpecialCharacter ? IsValidUpperSnakeCase<R> : false : true;
14
- type InvalidKeyError<K$1 extends string> = `Invalid error code key: "${K$1}" - must only contain uppercase letters (A-Z) and underscores (_)`;
15
- type ValidateErrorCodes<T> = { [K in keyof T]: K extends string ? IsValidUpperSnakeCase<K> extends false ? InvalidKeyError<K> : T[K] : T[K] };
16
- declare function defineErrorCodes<const T extends Record<string, string>>(codes: ValidateErrorCodes<T>): T;
17
- //#endregion
18
- //#region src/utils/id.d.ts
19
- declare const generateId: (size?: number) => string;
20
- //#endregion
21
- //#region src/utils/json.d.ts
22
- declare function safeJSONParse<T>(data: unknown): T | null;
23
- //#endregion
24
- //#region src/utils/string.d.ts
25
- declare function capitalizeFirstLetter(str: string): string;
26
- //#endregion
1
+ import { deprecate } from "./deprecate.mjs";
2
+ import { defineErrorCodes } from "./error-codes.mjs";
3
+ import { generateId } from "./id.mjs";
4
+ import { safeJSONParse } from "./json.mjs";
5
+ import { capitalizeFirstLetter } from "./string.mjs";
27
6
  export { capitalizeFirstLetter, defineErrorCodes, deprecate, generateId, safeJSONParse };
@@ -1,4 +1,7 @@
1
- import "../env-DbssmzoK.mjs";
2
- import { a as deprecate, i as defineErrorCodes, n as safeJSONParse, r as generateId, t as capitalizeFirstLetter } from "../utils-U2L7n92V.mjs";
1
+ import { deprecate } from "./deprecate.mjs";
2
+ import { defineErrorCodes } from "./error-codes.mjs";
3
+ import { generateId } from "./id.mjs";
4
+ import { safeJSONParse } from "./json.mjs";
5
+ import { capitalizeFirstLetter } from "./string.mjs";
3
6
 
4
7
  export { capitalizeFirstLetter, defineErrorCodes, deprecate, generateId, safeJSONParse };
@@ -0,0 +1,4 @@
1
+ //#region src/utils/json.d.ts
2
+ declare function safeJSONParse<T>(data: unknown): T | null;
3
+ //#endregion
4
+ export { safeJSONParse };
@@ -0,0 +1,25 @@
1
+ import { logger } from "../env/logger.mjs";
2
+ import "../env/index.mjs";
3
+
4
+ //#region src/utils/json.ts
5
+ function safeJSONParse(data) {
6
+ function reviver(_, value) {
7
+ if (typeof value === "string") {
8
+ if (/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z$/.test(value)) {
9
+ const date = new Date(value);
10
+ if (!isNaN(date.getTime())) return date;
11
+ }
12
+ }
13
+ return value;
14
+ }
15
+ try {
16
+ if (typeof data !== "string") return data;
17
+ return JSON.parse(data, reviver);
18
+ } catch (e) {
19
+ logger.error("Error parsing JSON", { error: e });
20
+ return null;
21
+ }
22
+ }
23
+
24
+ //#endregion
25
+ export { safeJSONParse };
@@ -0,0 +1,4 @@
1
+ //#region src/utils/string.d.ts
2
+ declare function capitalizeFirstLetter(str: string): string;
3
+ //#endregion
4
+ export { capitalizeFirstLetter };
@@ -0,0 +1,7 @@
1
+ //#region src/utils/string.ts
2
+ function capitalizeFirstLetter(str) {
3
+ return str.charAt(0).toUpperCase() + str.slice(1);
4
+ }
5
+
6
+ //#endregion
7
+ export { capitalizeFirstLetter };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@better-auth/core",
3
- "version": "1.4.12-beta.2",
3
+ "version": "1.4.13",
4
4
  "description": "The most comprehensive authentication framework for TypeScript.",
5
5
  "type": "module",
6
6
  "repository": {
@@ -2,6 +2,7 @@ import type { AsyncLocalStorage } from "@better-auth/core/async_hooks";
2
2
  import { getAsyncLocalStorage } from "@better-auth/core/async_hooks";
3
3
  import type { EndpointContext, InputContext } from "better-call";
4
4
  import type { AuthContext } from "../types";
5
+ import { __getBetterAuthGlobal } from "./global";
5
6
 
6
7
  export type AuthEndpointContext = Partial<
7
8
  InputContext<string, any> & EndpointContext<string, any>
@@ -9,24 +10,15 @@ export type AuthEndpointContext = Partial<
9
10
  context: AuthContext;
10
11
  };
11
12
 
12
- const symbol = Symbol.for("better-auth:endpoint-context-async-storage");
13
-
14
- let currentContextAsyncStorage: AsyncLocalStorage<AuthEndpointContext> | null =
15
- null;
16
-
17
13
  const ensureAsyncStorage = async () => {
18
- if (
19
- !currentContextAsyncStorage ||
20
- (globalThis as any)[symbol] === undefined
21
- ) {
14
+ const betterAuthGlobal = __getBetterAuthGlobal();
15
+ if (!betterAuthGlobal.context.endpointContextAsyncStorage) {
22
16
  const AsyncLocalStorage = await getAsyncLocalStorage();
23
- currentContextAsyncStorage = new AsyncLocalStorage();
24
- (globalThis as any)[symbol] = currentContextAsyncStorage;
17
+ betterAuthGlobal.context.endpointContextAsyncStorage =
18
+ new AsyncLocalStorage<AuthEndpointContext>();
25
19
  }
26
- return (
27
- currentContextAsyncStorage ||
28
- ((globalThis as any)[symbol] as AsyncLocalStorage<AuthEndpointContext>)
29
- );
20
+ return betterAuthGlobal.context
21
+ .endpointContextAsyncStorage as AsyncLocalStorage<AuthEndpointContext>;
30
22
  };
31
23
 
32
24
  /**
@@ -0,0 +1,57 @@
1
+ import type { AsyncLocalStorage } from "@better-auth/core/async_hooks";
2
+
3
+ interface BetterAuthGlobal {
4
+ /**
5
+ * The version of BetterAuth.
6
+ */
7
+ version: string;
8
+ /**
9
+ * Used to track the number of BetterAuth instances in the same process.
10
+ *
11
+ * Debugging purposes only.
12
+ */
13
+ epoch: number;
14
+ /**
15
+ * Stores the AsyncLocalStorage instances for each context.
16
+ */
17
+ context: Record<string, AsyncLocalStorage<unknown>>;
18
+ }
19
+
20
+ const symbol = Symbol.for("better-auth:global");
21
+ let bind: BetterAuthGlobal | null = null;
22
+
23
+ const __context: Record<string, AsyncLocalStorage<unknown>> = {};
24
+ const __betterAuthVersion: string = import.meta.env
25
+ .BETTER_AUTH_VERSION as string;
26
+
27
+ /**
28
+ * We store context instance in the globalThis.
29
+ *
30
+ * The reason we do this is that some bundlers, web framework, or package managers might
31
+ * create multiple copies of BetterAuth in the same process intentionally or unintentionally.
32
+ *
33
+ * For example, yarn v1, Next.js, SSR, Vite...
34
+ *
35
+ * @internal
36
+ */
37
+ export function __getBetterAuthGlobal(): BetterAuthGlobal {
38
+ if (!(globalThis as any)[symbol]) {
39
+ (globalThis as any)[symbol] = {
40
+ version: __betterAuthVersion,
41
+ epoch: 1,
42
+ context: __context,
43
+ };
44
+ bind = (globalThis as any)[symbol] as BetterAuthGlobal;
45
+ }
46
+ bind = (globalThis as any)[symbol] as BetterAuthGlobal;
47
+ if (bind.version !== __betterAuthVersion) {
48
+ bind.version = __betterAuthVersion;
49
+ // Different versions of BetterAuth are loaded in the same process.
50
+ bind.epoch++;
51
+ }
52
+ return (globalThis as any)[symbol] as BetterAuthGlobal;
53
+ }
54
+
55
+ export function getBetterAuthVersion(): string {
56
+ return __getBetterAuthGlobal().version;
57
+ }
@@ -4,6 +4,7 @@ export {
4
4
  getCurrentAuthContextAsyncLocalStorage,
5
5
  runWithEndpointContext,
6
6
  } from "./endpoint-context";
7
+ export { getBetterAuthVersion } from "./global";
7
8
  export {
8
9
  defineRequestState,
9
10
  getCurrentRequestState,
@@ -1,23 +1,18 @@
1
1
  import type { AsyncLocalStorage } from "@better-auth/core/async_hooks";
2
2
  import { getAsyncLocalStorage } from "@better-auth/core/async_hooks";
3
+ import { __getBetterAuthGlobal } from "./global";
3
4
 
4
5
  export type RequestStateWeakMap = WeakMap<object, any>;
5
6
 
6
- const symbol = Symbol.for("better-auth:request-state-async-storage");
7
-
8
- let requestStateAsyncStorage: AsyncLocalStorage<RequestStateWeakMap> | null =
9
- null;
10
-
11
7
  const ensureAsyncStorage = async () => {
12
- if (!requestStateAsyncStorage || (globalThis as any)[symbol] === undefined) {
8
+ const betterAuthGlobal = __getBetterAuthGlobal();
9
+ if (!betterAuthGlobal.context.requestStateAsyncStorage) {
13
10
  const AsyncLocalStorage = await getAsyncLocalStorage();
14
- requestStateAsyncStorage = new AsyncLocalStorage();
15
- (globalThis as any)[symbol] = requestStateAsyncStorage;
11
+ betterAuthGlobal.context.requestStateAsyncStorage =
12
+ new AsyncLocalStorage<RequestStateWeakMap>();
16
13
  }
17
- return (
18
- requestStateAsyncStorage ||
19
- ((globalThis as any)[symbol] as AsyncLocalStorage<RequestStateWeakMap>)
20
- );
14
+ return betterAuthGlobal.context
15
+ .requestStateAsyncStorage as AsyncLocalStorage<RequestStateWeakMap>;
21
16
  };
22
17
 
23
18
  export async function getRequestStateAsyncLocalStorage() {
@@ -1,25 +1,16 @@
1
- import type { AsyncLocalStorage } from "@better-auth/core/async_hooks";
1
+ import type { AsyncLocalStorage } from "node:async_hooks";
2
2
  import { getAsyncLocalStorage } from "@better-auth/core/async_hooks";
3
3
  import type { DBAdapter, DBTransactionAdapter } from "../db/adapter";
4
-
5
- const symbol = Symbol.for("better-auth:transaction-adapter-async-storage");
6
-
7
- let currentAdapterAsyncStorage: AsyncLocalStorage<DBTransactionAdapter> | null =
8
- null;
4
+ import { __getBetterAuthGlobal } from "./global";
9
5
 
10
6
  const ensureAsyncStorage = async () => {
11
- if (
12
- !currentAdapterAsyncStorage ||
13
- (globalThis as any)[symbol] === undefined
14
- ) {
7
+ const betterAuthGlobal = __getBetterAuthGlobal();
8
+ if (!betterAuthGlobal.context.adapterAsyncStorage) {
15
9
  const AsyncLocalStorage = await getAsyncLocalStorage();
16
- currentAdapterAsyncStorage = new AsyncLocalStorage();
17
- (globalThis as any)[symbol] = currentAdapterAsyncStorage;
10
+ betterAuthGlobal.context.adapterAsyncStorage = new AsyncLocalStorage();
18
11
  }
19
- return (
20
- currentAdapterAsyncStorage ||
21
- ((globalThis as any)[symbol] as AsyncLocalStorage<DBTransactionAdapter>)
22
- );
12
+ return betterAuthGlobal.context
13
+ .adapterAsyncStorage as AsyncLocalStorage<DBTransactionAdapter>;
23
14
  };
24
15
 
25
16
  /**