@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
@@ -200,7 +200,7 @@ export const createAdapterFactory =
200
200
  let value = data[field];
201
201
  const fieldAttributes = fields[field];
202
202
 
203
- let newFieldName: string =
203
+ const newFieldName: string =
204
204
  newMappedKeys[field] || fields[field]!.fieldName || field;
205
205
  if (
206
206
  value === undefined &&
@@ -335,7 +335,7 @@ export const createAdapterFactory =
335
335
  newValue = await field.transform.output(newValue);
336
336
  }
337
337
 
338
- let newFieldName: string = newMappedKeys[key] || key;
338
+ const newFieldName: string = newMappedKeys[key] || key;
339
339
 
340
340
  if (originalKey === "id" || field.references?.field === "id") {
341
341
  // Even if `useNumberId` is true, we must always return a string `id` output.
@@ -392,7 +392,7 @@ export const createAdapterFactory =
392
392
  unsafe_model = getDefaultModelName(unsafe_model);
393
393
  // for now we just transform the base model
394
394
  // later we append the joined models to this object.
395
- let transformedData: Record<string, any> = await transformSingleOutput(
395
+ const transformedData: Record<string, any> = await transformSingleOutput(
396
396
  data,
397
397
  unsafe_model,
398
398
  select,
@@ -443,7 +443,7 @@ export const createAdapterFactory =
443
443
  joinedData = [joinedData];
444
444
  }
445
445
 
446
- let transformed = [];
446
+ const transformed = [];
447
447
 
448
448
  if (Array.isArray(joinedData)) {
449
449
  for (const item of joinedData) {
@@ -822,7 +822,7 @@ export const createAdapterFactory =
822
822
  forceAllowId?: boolean;
823
823
  }): Promise<R> => {
824
824
  transactionId++;
825
- let thisTransactionId = transactionId;
825
+ const thisTransactionId = transactionId;
826
826
  const model = getModelName(unsafeModel);
827
827
  unsafeModel = getDefaultModelName(unsafeModel);
828
828
  if (
@@ -903,7 +903,7 @@ export const createAdapterFactory =
903
903
  update: Record<string, any>;
904
904
  }): Promise<T | null> => {
905
905
  transactionId++;
906
- let thisTransactionId = transactionId;
906
+ const thisTransactionId = transactionId;
907
907
  unsafeModel = getDefaultModelName(unsafeModel);
908
908
  const model = getModelName(unsafeModel);
909
909
  const where = transformWhereClause({
@@ -965,7 +965,7 @@ export const createAdapterFactory =
965
965
  update: Record<string, any>;
966
966
  }) => {
967
967
  transactionId++;
968
- let thisTransactionId = transactionId;
968
+ const thisTransactionId = transactionId;
969
969
  const model = getModelName(unsafeModel);
970
970
  const where = transformWhereClause({
971
971
  model: unsafeModel,
@@ -1021,7 +1021,7 @@ export const createAdapterFactory =
1021
1021
  join?: JoinOption;
1022
1022
  }) => {
1023
1023
  transactionId++;
1024
- let thisTransactionId = transactionId;
1024
+ const thisTransactionId = transactionId;
1025
1025
  const model = getModelName(unsafeModel);
1026
1026
  const where = transformWhereClause({
1027
1027
  model: unsafeModel,
@@ -1095,7 +1095,7 @@ export const createAdapterFactory =
1095
1095
  join?: JoinOption;
1096
1096
  }) => {
1097
1097
  transactionId++;
1098
- let thisTransactionId = transactionId;
1098
+ const thisTransactionId = transactionId;
1099
1099
  const limit =
1100
1100
  unsafeLimit ??
1101
1101
  options.advanced?.database?.defaultFindManyLimit ??
@@ -1173,7 +1173,7 @@ export const createAdapterFactory =
1173
1173
  where: Where[];
1174
1174
  }) => {
1175
1175
  transactionId++;
1176
- let thisTransactionId = transactionId;
1176
+ const thisTransactionId = transactionId;
1177
1177
  const model = getModelName(unsafeModel);
1178
1178
  const where = transformWhereClause({
1179
1179
  model: unsafeModel,
@@ -1206,7 +1206,7 @@ export const createAdapterFactory =
1206
1206
  where: Where[];
1207
1207
  }) => {
1208
1208
  transactionId++;
1209
- let thisTransactionId = transactionId;
1209
+ const thisTransactionId = transactionId;
1210
1210
  const model = getModelName(unsafeModel);
1211
1211
  const where = transformWhereClause({
1212
1212
  model: unsafeModel,
@@ -1240,7 +1240,7 @@ export const createAdapterFactory =
1240
1240
  where?: Where[];
1241
1241
  }) => {
1242
1242
  transactionId++;
1243
- let thisTransactionId = transactionId;
1243
+ const thisTransactionId = transactionId;
1244
1244
  const model = getModelName(unsafeModel);
1245
1245
  const where = transformWhereClause({
1246
1246
  model: unsafeModel,
@@ -1350,7 +1350,7 @@ export const createAdapterFactory =
1350
1350
  }
1351
1351
 
1352
1352
  //`${colors.fg.blue}|${colors.reset} `,
1353
- let log: any[] = logs
1353
+ const log: any[] = logs
1354
1354
  .reverse()
1355
1355
  .map((log) => {
1356
1356
  log.args[0] = `\n${log.args[0]}`;
@@ -23,7 +23,7 @@ export const initGetDefaultModelName = ({
23
23
  // It's possible this `model` could had applied `usePlural`.
24
24
  // Thus we'll try the search but without the trailing `s`.
25
25
  if (usePlural && model.charAt(model.length - 1) === "s") {
26
- let pluralessModel = model.slice(0, -1);
26
+ const pluralessModel = model.slice(0, -1);
27
27
  let m = schema[pluralessModel] ? pluralessModel : undefined;
28
28
  if (!m) {
29
29
  m = Object.entries(schema).find(
@@ -36,7 +36,7 @@ export const initGetIdField = ({
36
36
  options.advanced?.database?.generateId === "serial";
37
37
  const useUUIDs = options.advanced?.database?.generateId === "uuid";
38
38
 
39
- let shouldGenerateId: boolean = (() => {
39
+ const shouldGenerateId: boolean = (() => {
40
40
  if (disableIdGeneration) {
41
41
  return false;
42
42
  } else if (useNumberId && !forceAllowId) {
@@ -58,7 +58,7 @@ export const initGetIdField = ({
58
58
  ? {
59
59
  defaultValue() {
60
60
  if (disableIdGeneration) return undefined;
61
- let generateId = options.advanced?.database?.generateId;
61
+ const generateId = options.advanced?.database?.generateId;
62
62
  if (generateId === false || useNumberId) return undefined;
63
63
  if (typeof generateId === "function") {
64
64
  return generateId({
@@ -1,9 +1,8 @@
1
1
  export class BetterAuthError extends Error {
2
- constructor(message: string, cause?: string | undefined) {
3
- super(message);
2
+ constructor(message: string, options?: { cause?: unknown | undefined }) {
3
+ super(message, options);
4
4
  this.name = "BetterAuthError";
5
5
  this.message = message;
6
- this.cause = cause;
7
6
  this.stack = "";
8
7
  }
9
8
  }
@@ -65,7 +65,7 @@ const cleanDoubleSlashes = (input: string = "") => {
65
65
  };
66
66
 
67
67
  const issuerToEndpoints = (issuer?: string | undefined) => {
68
- let baseUrl = issuer || "https://gitlab.com";
68
+ const baseUrl = issuer || "https://gitlab.com";
69
69
  return {
70
70
  authorizationEndpoint: cleanDoubleSlashes(`${baseUrl}/oauth/authorize`),
71
71
  tokenEndpoint: cleanDoubleSlashes(`${baseUrl}/oauth/token`),
@@ -11,7 +11,7 @@ import type {
11
11
  import type { DBAdapter, Where } from "../db/adapter";
12
12
  import type { createLogger } from "../env";
13
13
  import type { OAuthProvider } from "../oauth2";
14
- import type { BetterAuthCookies } from "./cookie";
14
+ import type { BetterAuthCookie, BetterAuthCookies } from "./cookie";
15
15
  import type {
16
16
  BetterAuthOptions,
17
17
  BetterAuthRateLimitOptions,
@@ -150,10 +150,7 @@ export interface InternalAdapter<
150
150
  type CreateCookieGetterFn = (
151
151
  cookieName: string,
152
152
  overrideAttributes?: Partial<CookieOptions> | undefined,
153
- ) => {
154
- name: string;
155
- attributes: CookieOptions;
156
- };
153
+ ) => BetterAuthCookie;
157
154
 
158
155
  type CheckPasswordFn<Options extends BetterAuthOptions = BetterAuthOptions> = (
159
156
  userId: string,
@@ -166,139 +163,148 @@ export type PluginContext = {
166
163
  ) => Plugin | null;
167
164
  };
168
165
 
166
+ export type InfoContext = {
167
+ appName: string;
168
+ baseURL: string;
169
+ version: string;
170
+ };
171
+
169
172
  export type AuthContext<Options extends BetterAuthOptions = BetterAuthOptions> =
170
- PluginContext & {
171
- options: Options;
172
- appName: string;
173
- baseURL: string;
174
- trustedOrigins: string[];
175
- /**
176
- * Verifies whether url is a trusted origin according to the "trustedOrigins" configuration
177
- * @param url The url to verify against the "trustedOrigins" configuration
178
- * @param settings Specify supported pattern matching settings
179
- * @returns {boolean} true if the URL matches the origin pattern, false otherwise.
180
- */
181
- isTrustedOrigin: (
182
- url: string,
183
- settings?: { allowRelativePaths: boolean },
184
- ) => boolean;
185
- oauthConfig: {
173
+ PluginContext &
174
+ InfoContext & {
175
+ options: Options;
176
+ trustedOrigins: string[];
186
177
  /**
187
- * This is dangerous and should only be used in dev or staging environments.
178
+ * Verifies whether url is a trusted origin according to the "trustedOrigins" configuration
179
+ * @param url The url to verify against the "trustedOrigins" configuration
180
+ * @param settings Specify supported pattern matching settings
181
+ * @returns {boolean} true if the URL matches the origin pattern, false otherwise.
188
182
  */
189
- skipStateCookieCheck?: boolean | undefined;
183
+ isTrustedOrigin: (
184
+ url: string,
185
+ settings?: { allowRelativePaths: boolean },
186
+ ) => boolean;
187
+ oauthConfig: {
188
+ /**
189
+ * This is dangerous and should only be used in dev or staging environments.
190
+ */
191
+ skipStateCookieCheck?: boolean | undefined;
192
+ /**
193
+ * Strategy for storing OAuth state
194
+ *
195
+ * - "cookie": Store state in an encrypted cookie (stateless)
196
+ * - "database": Store state in the database
197
+ *
198
+ * @default "cookie"
199
+ */
200
+ storeStateStrategy: "database" | "cookie";
201
+ };
190
202
  /**
191
- * Strategy for storing OAuth state
192
- *
193
- * - "cookie": Store state in an encrypted cookie (stateless)
194
- * - "database": Store state in the database
195
- *
196
- * @default "cookie"
203
+ * New session that will be set after the request
204
+ * meaning: there is a `set-cookie` header that will set
205
+ * the session cookie. This is the fetched session. And it's set
206
+ * by `setNewSession` method.
197
207
  */
198
- storeStateStrategy: "database" | "cookie";
199
- };
200
- /**
201
- * New session that will be set after the request
202
- * meaning: there is a `set-cookie` header that will set
203
- * the session cookie. This is the fetched session. And it's set
204
- * by `setNewSession` method.
205
- */
206
- newSession: {
207
- session: Session & Record<string, any>;
208
- user: User & Record<string, any>;
209
- } | null;
210
- session: {
211
- session: Session & Record<string, any>;
212
- user: User & Record<string, any>;
213
- } | null;
214
- setNewSession: (
208
+ newSession: {
209
+ session: Session & Record<string, any>;
210
+ user: User & Record<string, any>;
211
+ } | null;
215
212
  session: {
216
213
  session: Session & Record<string, any>;
217
214
  user: User & Record<string, any>;
218
- } | null,
219
- ) => void;
220
- socialProviders: OAuthProvider[];
221
- authCookies: BetterAuthCookies;
222
- logger: ReturnType<typeof createLogger>;
223
- rateLimit: {
224
- enabled: boolean;
225
- window: number;
226
- max: number;
227
- storage: "memory" | "database" | "secondary-storage";
228
- } & BetterAuthRateLimitOptions;
229
- adapter: DBAdapter<Options>;
230
- internalAdapter: InternalAdapter<Options>;
231
- createAuthCookie: CreateCookieGetterFn;
232
- secret: string;
233
- sessionConfig: {
234
- updateAge: number;
235
- expiresIn: number;
236
- freshAge: number;
237
- cookieRefreshCache:
238
- | false
239
- | {
240
- enabled: true;
241
- updateAge: number;
242
- };
243
- };
244
- generateId: (options: {
245
- model: ModelNames;
246
- size?: number | undefined;
247
- }) => string | false;
248
- secondaryStorage: SecondaryStorage | undefined;
249
- password: {
250
- hash: (password: string) => Promise<string>;
251
- verify: (data: { password: string; hash: string }) => Promise<boolean>;
252
- config: {
253
- minPasswordLength: number;
254
- maxPasswordLength: number;
215
+ } | null;
216
+ setNewSession: (
217
+ session: {
218
+ session: Session & Record<string, any>;
219
+ user: User & Record<string, any>;
220
+ } | null,
221
+ ) => void;
222
+ socialProviders: OAuthProvider[];
223
+ authCookies: BetterAuthCookies;
224
+ logger: ReturnType<typeof createLogger>;
225
+ rateLimit: {
226
+ enabled: boolean;
227
+ window: number;
228
+ max: number;
229
+ storage: "memory" | "database" | "secondary-storage";
230
+ } & Omit<
231
+ BetterAuthRateLimitOptions,
232
+ "enabled" | "window" | "max" | "storage"
233
+ >;
234
+ adapter: DBAdapter<Options>;
235
+ internalAdapter: InternalAdapter<Options>;
236
+ createAuthCookie: CreateCookieGetterFn;
237
+ secret: string;
238
+ sessionConfig: {
239
+ updateAge: number;
240
+ expiresIn: number;
241
+ freshAge: number;
242
+ cookieRefreshCache:
243
+ | false
244
+ | {
245
+ enabled: true;
246
+ updateAge: number;
247
+ };
255
248
  };
256
- checkPassword: CheckPasswordFn<Options>;
249
+ generateId: (options: {
250
+ model: ModelNames;
251
+ size?: number | undefined;
252
+ }) => string | false;
253
+ secondaryStorage: SecondaryStorage | undefined;
254
+ password: {
255
+ hash: (password: string) => Promise<string>;
256
+ verify: (data: { password: string; hash: string }) => Promise<boolean>;
257
+ config: {
258
+ minPasswordLength: number;
259
+ maxPasswordLength: number;
260
+ };
261
+ checkPassword: CheckPasswordFn<Options>;
262
+ };
263
+ tables: BetterAuthDBSchema;
264
+ runMigrations: () => Promise<void>;
265
+ publishTelemetry: (event: {
266
+ type: string;
267
+ anonymousId?: string | undefined;
268
+ payload: Record<string, any>;
269
+ }) => Promise<void>;
270
+ /**
271
+ * Skip origin check for requests.
272
+ *
273
+ * - `true`: Skip for ALL requests (DANGEROUS - disables CSRF protection)
274
+ * - `string[]`: Skip only for specific paths (e.g., SAML callbacks)
275
+ * - `false`: Enable origin check (default)
276
+ *
277
+ * Paths support prefix matching (e.g., "/sso/saml2/callback" matches
278
+ * "/sso/saml2/callback/provider-name").
279
+ *
280
+ * @default false (true in test environments)
281
+ */
282
+ skipOriginCheck: boolean | string[];
283
+ /**
284
+ * This skips the CSRF check for all requests.
285
+ *
286
+ * This is inferred from the `options.advanced?.
287
+ * disableCSRFCheck` option.
288
+ *
289
+ * @default false
290
+ */
291
+ skipCSRFCheck: boolean;
292
+ /**
293
+ * Background task handler for deferred operations.
294
+ *
295
+ * This is inferred from the `options.advanced?.backgroundTasks?.handler` option.
296
+ * Defaults to a no-op that just runs the promise.
297
+ */
298
+ runInBackground: (promise: Promise<void>) => void;
299
+ /**
300
+ * Runs a task in the background if `runInBackground` is configured,
301
+ * otherwise awaits the task directly.
302
+ *
303
+ * This is useful for operations like sending emails where we want
304
+ * to avoid blocking the response when possible (for timing attack
305
+ * mitigation), but still ensure the operation completes.
306
+ */
307
+ runInBackgroundOrAwait: (
308
+ promise: Promise<unknown> | Promise<void> | void | unknown,
309
+ ) => Promise<unknown>;
257
310
  };
258
- tables: BetterAuthDBSchema;
259
- runMigrations: () => Promise<void>;
260
- publishTelemetry: (event: {
261
- type: string;
262
- anonymousId?: string | undefined;
263
- payload: Record<string, any>;
264
- }) => Promise<void>;
265
- /**
266
- * This skips the origin check for all requests.
267
- *
268
- * set to true by default for `test` environments and `false`
269
- * for other environments.
270
- *
271
- * It's inferred from the `options.advanced?.disableCSRFCheck`
272
- * option or `options.advanced?.disableOriginCheck` option.
273
- *
274
- * @default false
275
- */
276
- skipOriginCheck: boolean;
277
- /**
278
- * This skips the CSRF check for all requests.
279
- *
280
- * This is inferred from the `options.advanced?.
281
- * disableCSRFCheck` option.
282
- *
283
- * @default false
284
- */
285
- skipCSRFCheck: boolean;
286
- /**
287
- * Background task handler for deferred operations.
288
- *
289
- * This is inferred from the `options.advanced?.backgroundTasks?.handler` option.
290
- * Defaults to a no-op that just runs the promise.
291
- */
292
- runInBackground: (promise: Promise<void>) => void;
293
- /**
294
- * Runs a task in the background if `runInBackground` is configured,
295
- * otherwise awaits the task directly.
296
- *
297
- * This is useful for operations like sending emails where we want
298
- * to avoid blocking the response when possible (for timing attack
299
- * mitigation), but still ensure the operation completes.
300
- */
301
- runInBackgroundOrAwait: (
302
- promise: Promise<unknown> | Promise<void> | void | unknown,
303
- ) => Promise<unknown>;
304
- };
@@ -1,8 +1,10 @@
1
1
  import type { CookieOptions } from "better-call";
2
2
 
3
+ export type BetterAuthCookie = { name: string; attributes: CookieOptions };
4
+
3
5
  export type BetterAuthCookies = {
4
- sessionToken: { name: string; options: CookieOptions };
5
- sessionData: { name: string; options: CookieOptions };
6
- accountData: { name: string; options: CookieOptions };
7
- dontRememberToken: { name: string; options: CookieOptions };
6
+ sessionToken: BetterAuthCookie;
7
+ sessionData: BetterAuthCookie;
8
+ accountData: BetterAuthCookie;
9
+ dontRememberToken: BetterAuthCookie;
8
10
  };
@@ -2,10 +2,11 @@ export type { StandardSchemaV1 } from "@standard-schema/spec";
2
2
  export type {
3
3
  AuthContext,
4
4
  GenericEndpointContext,
5
+ InfoContext,
5
6
  InternalAdapter,
6
7
  PluginContext,
7
8
  } from "./context";
8
- export type { BetterAuthCookies } from "./cookie";
9
+ export type { BetterAuthCookie, BetterAuthCookies } from "./cookie";
9
10
  export type * from "./helper";
10
11
  export type {
11
12
  BetterAuthAdvancedOptions,
package/tsdown.config.ts CHANGED
@@ -1,5 +1,10 @@
1
+ import { readFile } from "node:fs/promises";
1
2
  import { defineConfig } from "tsdown";
2
3
 
4
+ const packageJson = JSON.parse(
5
+ await readFile(new URL("./package.json", import.meta.url), "utf-8"),
6
+ );
7
+
3
8
  export default defineConfig({
4
9
  dts: { build: true, incremental: true },
5
10
  format: ["esm"],
@@ -18,5 +23,9 @@ export default defineConfig({
18
23
  "./src/error/index.ts",
19
24
  ],
20
25
  external: ["@better-auth/core/async_hooks"],
26
+ env: {
27
+ BETTER_AUTH_VERSION: packageJson.version,
28
+ },
29
+ unbundle: true,
21
30
  clean: true,
22
31
  });
@@ -1,126 +0,0 @@
1
- import { getAsyncLocalStorage } from "@better-auth/core/async_hooks";
2
-
3
- //#region src/context/endpoint-context.ts
4
- const symbol$2 = Symbol.for("better-auth:endpoint-context-async-storage");
5
- let currentContextAsyncStorage = null;
6
- const ensureAsyncStorage$2 = async () => {
7
- if (!currentContextAsyncStorage || globalThis[symbol$2] === void 0) {
8
- currentContextAsyncStorage = new (await (getAsyncLocalStorage()))();
9
- globalThis[symbol$2] = currentContextAsyncStorage;
10
- }
11
- return currentContextAsyncStorage || globalThis[symbol$2];
12
- };
13
- /**
14
- * This is for internal use only. Most users should use `getCurrentAuthContext` instead.
15
- *
16
- * It is exposed for advanced use cases where you need direct access to the AsyncLocalStorage instance.
17
- */
18
- async function getCurrentAuthContextAsyncLocalStorage() {
19
- return ensureAsyncStorage$2();
20
- }
21
- async function getCurrentAuthContext() {
22
- const context = (await ensureAsyncStorage$2()).getStore();
23
- if (!context) throw new Error("No auth context found. Please make sure you are calling this function within a `runWithEndpointContext` callback.");
24
- return context;
25
- }
26
- async function runWithEndpointContext(context, fn) {
27
- return (await ensureAsyncStorage$2()).run(context, fn);
28
- }
29
-
30
- //#endregion
31
- //#region src/context/request-state.ts
32
- const symbol$1 = Symbol.for("better-auth:request-state-async-storage");
33
- let requestStateAsyncStorage = null;
34
- const ensureAsyncStorage$1 = async () => {
35
- if (!requestStateAsyncStorage || globalThis[symbol$1] === void 0) {
36
- requestStateAsyncStorage = new (await (getAsyncLocalStorage()))();
37
- globalThis[symbol$1] = requestStateAsyncStorage;
38
- }
39
- return requestStateAsyncStorage || globalThis[symbol$1];
40
- };
41
- async function getRequestStateAsyncLocalStorage() {
42
- return ensureAsyncStorage$1();
43
- }
44
- async function hasRequestState() {
45
- return (await ensureAsyncStorage$1()).getStore() !== void 0;
46
- }
47
- async function getCurrentRequestState() {
48
- const store = (await ensureAsyncStorage$1()).getStore();
49
- if (!store) throw new Error("No request state found. Please make sure you are calling this function within a `runWithRequestState` callback.");
50
- return store;
51
- }
52
- async function runWithRequestState(store, fn) {
53
- return (await ensureAsyncStorage$1()).run(store, fn);
54
- }
55
- function defineRequestState(initFn) {
56
- const ref = Object.freeze({});
57
- return {
58
- get ref() {
59
- return ref;
60
- },
61
- async get() {
62
- const store = await getCurrentRequestState();
63
- if (!store.has(ref)) {
64
- const initialValue = await initFn();
65
- store.set(ref, initialValue);
66
- return initialValue;
67
- }
68
- return store.get(ref);
69
- },
70
- async set(value) {
71
- (await getCurrentRequestState()).set(ref, value);
72
- }
73
- };
74
- }
75
-
76
- //#endregion
77
- //#region src/context/transaction.ts
78
- const symbol = Symbol.for("better-auth:transaction-adapter-async-storage");
79
- let currentAdapterAsyncStorage = null;
80
- const ensureAsyncStorage = async () => {
81
- if (!currentAdapterAsyncStorage || globalThis[symbol] === void 0) {
82
- currentAdapterAsyncStorage = new (await (getAsyncLocalStorage()))();
83
- globalThis[symbol] = currentAdapterAsyncStorage;
84
- }
85
- return currentAdapterAsyncStorage || globalThis[symbol];
86
- };
87
- /**
88
- * This is for internal use only. Most users should use `getCurrentAdapter` instead.
89
- *
90
- * It is exposed for advanced use cases where you need direct access to the AsyncLocalStorage instance.
91
- */
92
- const getCurrentDBAdapterAsyncLocalStorage = async () => {
93
- return ensureAsyncStorage();
94
- };
95
- const getCurrentAdapter = async (fallback) => {
96
- return ensureAsyncStorage().then((als) => {
97
- return als.getStore() || fallback;
98
- }).catch(() => {
99
- return fallback;
100
- });
101
- };
102
- const runWithAdapter = async (adapter, fn) => {
103
- let called = true;
104
- return ensureAsyncStorage().then((als) => {
105
- called = true;
106
- return als.run(adapter, fn);
107
- }).catch((err) => {
108
- if (!called) return fn();
109
- throw err;
110
- });
111
- };
112
- const runWithTransaction = async (adapter, fn) => {
113
- let called = true;
114
- return ensureAsyncStorage().then((als) => {
115
- called = true;
116
- return adapter.transaction(async (trx) => {
117
- return als.run(trx, fn);
118
- });
119
- }).catch((err) => {
120
- if (!called) return fn();
121
- throw err;
122
- });
123
- };
124
-
125
- //#endregion
126
- export { defineRequestState as a, hasRequestState as c, getCurrentAuthContextAsyncLocalStorage as d, runWithEndpointContext as f, runWithTransaction as i, runWithRequestState as l, getCurrentDBAdapterAsyncLocalStorage as n, getCurrentRequestState as o, runWithAdapter as r, getRequestStateAsyncLocalStorage as s, getCurrentAdapter as t, getCurrentAuthContext as u };