@better-auth/core 1.5.0-beta.9 → 1.5.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.
Files changed (306) hide show
  1. package/README.md +17 -0
  2. package/dist/api/index.d.mts +144 -41
  3. package/dist/api/index.mjs +2 -1
  4. package/dist/api/index.mjs.map +1 -0
  5. package/dist/async_hooks/index.d.mts +2 -1
  6. package/dist/async_hooks/index.mjs +2 -1
  7. package/dist/async_hooks/index.mjs.map +1 -0
  8. package/dist/async_hooks/pure.index.d.mts +2 -1
  9. package/dist/async_hooks/pure.index.mjs +2 -1
  10. package/dist/async_hooks/pure.index.mjs.map +1 -0
  11. package/dist/context/endpoint-context.d.mts +2 -1
  12. package/dist/context/endpoint-context.mjs +4 -3
  13. package/dist/context/endpoint-context.mjs.map +1 -0
  14. package/dist/context/global.d.mts +2 -2
  15. package/dist/context/global.mjs +3 -2
  16. package/dist/context/global.mjs.map +1 -0
  17. package/dist/context/request-state.d.mts +2 -1
  18. package/dist/context/request-state.mjs +4 -3
  19. package/dist/context/request-state.mjs.map +1 -0
  20. package/dist/context/transaction.d.mts +2 -1
  21. package/dist/context/transaction.mjs +4 -3
  22. package/dist/context/transaction.mjs.map +1 -0
  23. package/dist/db/adapter/factory.d.mts +6 -13
  24. package/dist/db/adapter/factory.mjs +44 -57
  25. package/dist/db/adapter/factory.mjs.map +1 -0
  26. package/dist/db/adapter/get-default-field-name.d.mts +2 -1
  27. package/dist/db/adapter/get-default-field-name.mjs +3 -2
  28. package/dist/db/adapter/get-default-field-name.mjs.map +1 -0
  29. package/dist/db/adapter/get-default-model-name.d.mts +2 -1
  30. package/dist/db/adapter/get-default-model-name.mjs +5 -4
  31. package/dist/db/adapter/get-default-model-name.mjs.map +1 -0
  32. package/dist/db/adapter/get-field-attributes.d.mts +3 -2
  33. package/dist/db/adapter/get-field-attributes.mjs +2 -1
  34. package/dist/db/adapter/get-field-attributes.mjs.map +1 -0
  35. package/dist/db/adapter/get-field-name.d.mts +2 -1
  36. package/dist/db/adapter/get-field-name.mjs +2 -1
  37. package/dist/db/adapter/get-field-name.mjs.map +1 -0
  38. package/dist/db/adapter/get-id-field.d.mts +3 -2
  39. package/dist/db/adapter/get-id-field.mjs +3 -2
  40. package/dist/db/adapter/get-id-field.mjs.map +1 -0
  41. package/dist/db/adapter/get-model-name.d.mts +2 -1
  42. package/dist/db/adapter/get-model-name.mjs +2 -1
  43. package/dist/db/adapter/get-model-name.mjs.map +1 -0
  44. package/dist/db/adapter/index.d.mts +10 -4
  45. package/dist/db/adapter/index.mjs +19 -2
  46. package/dist/db/adapter/index.mjs.map +1 -0
  47. package/dist/db/adapter/types.d.mts +3 -34
  48. package/dist/db/adapter/utils.d.mts +2 -1
  49. package/dist/db/adapter/utils.mjs +2 -1
  50. package/dist/db/adapter/utils.mjs.map +1 -0
  51. package/dist/db/get-tables.d.mts +2 -1
  52. package/dist/db/get-tables.mjs +46 -39
  53. package/dist/db/get-tables.mjs.map +1 -0
  54. package/dist/db/index.d.mts +7 -7
  55. package/dist/db/plugin.d.mts +2 -1
  56. package/dist/db/schema/account.d.mts +8 -4
  57. package/dist/db/schema/account.mjs +2 -1
  58. package/dist/db/schema/account.mjs.map +1 -0
  59. package/dist/db/schema/rate-limit.d.mts +8 -2
  60. package/dist/db/schema/rate-limit.mjs +2 -1
  61. package/dist/db/schema/rate-limit.mjs.map +1 -0
  62. package/dist/db/schema/session.d.mts +8 -4
  63. package/dist/db/schema/session.mjs +2 -1
  64. package/dist/db/schema/session.mjs.map +1 -0
  65. package/dist/db/schema/shared.d.mts +2 -1
  66. package/dist/db/schema/shared.mjs +2 -1
  67. package/dist/db/schema/shared.mjs.map +1 -0
  68. package/dist/db/schema/user.d.mts +8 -4
  69. package/dist/db/schema/user.mjs +2 -1
  70. package/dist/db/schema/user.mjs.map +1 -0
  71. package/dist/db/schema/verification.d.mts +8 -4
  72. package/dist/db/schema/verification.mjs +2 -1
  73. package/dist/db/schema/verification.mjs.map +1 -0
  74. package/dist/db/type.d.mts +28 -2
  75. package/dist/env/color-depth.d.mts +2 -1
  76. package/dist/env/color-depth.mjs +2 -1
  77. package/dist/env/color-depth.mjs.map +1 -0
  78. package/dist/env/env-impl.d.mts +3 -2
  79. package/dist/env/env-impl.mjs +9 -8
  80. package/dist/env/env-impl.mjs.map +1 -0
  81. package/dist/env/logger.d.mts +2 -1
  82. package/dist/env/logger.mjs +3 -2
  83. package/dist/env/logger.mjs.map +1 -0
  84. package/dist/error/codes.d.mts +64 -181
  85. package/dist/error/codes.mjs +6 -2
  86. package/dist/error/codes.mjs.map +1 -0
  87. package/dist/error/index.d.mts +2 -1
  88. package/dist/error/index.mjs +2 -1
  89. package/dist/error/index.mjs.map +1 -0
  90. package/dist/index.d.mts +5 -4
  91. package/dist/oauth2/client-credentials-token.d.mts +25 -3
  92. package/dist/oauth2/client-credentials-token.mjs +15 -2
  93. package/dist/oauth2/client-credentials-token.mjs.map +1 -0
  94. package/dist/oauth2/create-authorization-url.d.mts +5 -2
  95. package/dist/oauth2/create-authorization-url.mjs +3 -1
  96. package/dist/oauth2/create-authorization-url.mjs.map +1 -0
  97. package/dist/oauth2/index.d.mts +4 -4
  98. package/dist/oauth2/index.mjs +4 -4
  99. package/dist/oauth2/oauth-provider.d.mts +3 -2
  100. package/dist/oauth2/refresh-access-token.d.mts +24 -4
  101. package/dist/oauth2/refresh-access-token.mjs +20 -2
  102. package/dist/oauth2/refresh-access-token.mjs.map +1 -0
  103. package/dist/oauth2/utils.d.mts +2 -1
  104. package/dist/oauth2/utils.mjs +2 -1
  105. package/dist/oauth2/utils.mjs.map +1 -0
  106. package/dist/oauth2/validate-authorization-code.d.mts +37 -4
  107. package/dist/oauth2/validate-authorization-code.mjs +25 -13
  108. package/dist/oauth2/validate-authorization-code.mjs.map +1 -0
  109. package/dist/oauth2/verify.d.mts +7 -13
  110. package/dist/oauth2/verify.mjs +2 -1
  111. package/dist/oauth2/verify.mjs.map +1 -0
  112. package/dist/social-providers/apple.d.mts +2 -1
  113. package/dist/social-providers/apple.mjs +22 -21
  114. package/dist/social-providers/apple.mjs.map +1 -0
  115. package/dist/social-providers/atlassian.d.mts +2 -1
  116. package/dist/social-providers/atlassian.mjs +2 -1
  117. package/dist/social-providers/atlassian.mjs.map +1 -0
  118. package/dist/social-providers/cognito.d.mts +2 -1
  119. package/dist/social-providers/cognito.mjs +4 -3
  120. package/dist/social-providers/cognito.mjs.map +1 -0
  121. package/dist/social-providers/discord.d.mts +2 -1
  122. package/dist/social-providers/discord.mjs +2 -1
  123. package/dist/social-providers/discord.mjs.map +1 -0
  124. package/dist/social-providers/dropbox.d.mts +2 -1
  125. package/dist/social-providers/dropbox.mjs +2 -1
  126. package/dist/social-providers/dropbox.mjs.map +1 -0
  127. package/dist/social-providers/facebook.d.mts +2 -1
  128. package/dist/social-providers/facebook.mjs +13 -12
  129. package/dist/social-providers/facebook.mjs.map +1 -0
  130. package/dist/social-providers/figma.d.mts +2 -1
  131. package/dist/social-providers/figma.mjs +2 -1
  132. package/dist/social-providers/figma.mjs.map +1 -0
  133. package/dist/social-providers/github.d.mts +3 -2
  134. package/dist/social-providers/github.mjs +23 -6
  135. package/dist/social-providers/github.mjs.map +1 -0
  136. package/dist/social-providers/gitlab.d.mts +2 -1
  137. package/dist/social-providers/gitlab.mjs +3 -2
  138. package/dist/social-providers/gitlab.mjs.map +1 -0
  139. package/dist/social-providers/google.d.mts +2 -1
  140. package/dist/social-providers/google.mjs +18 -13
  141. package/dist/social-providers/google.mjs.map +1 -0
  142. package/dist/social-providers/huggingface.d.mts +2 -1
  143. package/dist/social-providers/huggingface.mjs +3 -2
  144. package/dist/social-providers/huggingface.mjs.map +1 -0
  145. package/dist/social-providers/index.d.mts +61 -8
  146. package/dist/social-providers/index.mjs +5 -2
  147. package/dist/social-providers/index.mjs.map +1 -0
  148. package/dist/social-providers/kakao.d.mts +3 -2
  149. package/dist/social-providers/kakao.mjs +3 -2
  150. package/dist/social-providers/kakao.mjs.map +1 -0
  151. package/dist/social-providers/kick.d.mts +2 -1
  152. package/dist/social-providers/kick.mjs +2 -1
  153. package/dist/social-providers/kick.mjs.map +1 -0
  154. package/dist/social-providers/line.d.mts +2 -1
  155. package/dist/social-providers/line.mjs +3 -2
  156. package/dist/social-providers/line.mjs.map +1 -0
  157. package/dist/social-providers/linear.d.mts +2 -1
  158. package/dist/social-providers/linear.mjs +2 -1
  159. package/dist/social-providers/linear.mjs.map +1 -0
  160. package/dist/social-providers/linkedin.d.mts +2 -1
  161. package/dist/social-providers/linkedin.mjs +2 -1
  162. package/dist/social-providers/linkedin.mjs.map +1 -0
  163. package/dist/social-providers/microsoft-entra-id.d.mts +4 -1
  164. package/dist/social-providers/microsoft-entra-id.mjs +36 -2
  165. package/dist/social-providers/microsoft-entra-id.mjs.map +1 -0
  166. package/dist/social-providers/naver.d.mts +11 -20
  167. package/dist/social-providers/naver.mjs +3 -2
  168. package/dist/social-providers/naver.mjs.map +1 -0
  169. package/dist/social-providers/notion.d.mts +2 -1
  170. package/dist/social-providers/notion.mjs +3 -2
  171. package/dist/social-providers/notion.mjs.map +1 -0
  172. package/dist/social-providers/paybin.d.mts +2 -1
  173. package/dist/social-providers/paybin.mjs +3 -2
  174. package/dist/social-providers/paybin.mjs.map +1 -0
  175. package/dist/social-providers/paypal.d.mts +2 -1
  176. package/dist/social-providers/paypal.mjs +2 -1
  177. package/dist/social-providers/paypal.mjs.map +1 -0
  178. package/dist/social-providers/polar.d.mts +2 -1
  179. package/dist/social-providers/polar.mjs +3 -2
  180. package/dist/social-providers/polar.mjs.map +1 -0
  181. package/dist/social-providers/railway.d.mts +68 -0
  182. package/dist/social-providers/railway.mjs +78 -0
  183. package/dist/social-providers/railway.mjs.map +1 -0
  184. package/dist/social-providers/reddit.d.mts +2 -1
  185. package/dist/social-providers/reddit.mjs +2 -1
  186. package/dist/social-providers/reddit.mjs.map +1 -0
  187. package/dist/social-providers/roblox.d.mts +2 -1
  188. package/dist/social-providers/roblox.mjs +2 -1
  189. package/dist/social-providers/roblox.mjs.map +1 -0
  190. package/dist/social-providers/salesforce.d.mts +2 -1
  191. package/dist/social-providers/salesforce.mjs +2 -1
  192. package/dist/social-providers/salesforce.mjs.map +1 -0
  193. package/dist/social-providers/slack.d.mts +2 -1
  194. package/dist/social-providers/slack.mjs +2 -1
  195. package/dist/social-providers/slack.mjs.map +1 -0
  196. package/dist/social-providers/spotify.d.mts +2 -1
  197. package/dist/social-providers/spotify.mjs +2 -1
  198. package/dist/social-providers/spotify.mjs.map +1 -0
  199. package/dist/social-providers/tiktok.d.mts +3 -3
  200. package/dist/social-providers/tiktok.mjs +3 -2
  201. package/dist/social-providers/tiktok.mjs.map +1 -0
  202. package/dist/social-providers/twitch.d.mts +2 -1
  203. package/dist/social-providers/twitch.mjs +2 -1
  204. package/dist/social-providers/twitch.mjs.map +1 -0
  205. package/dist/social-providers/twitter.d.mts +14 -25
  206. package/dist/social-providers/twitter.mjs +2 -1
  207. package/dist/social-providers/twitter.mjs.map +1 -0
  208. package/dist/social-providers/vercel.d.mts +2 -1
  209. package/dist/social-providers/vercel.mjs +3 -2
  210. package/dist/social-providers/vercel.mjs.map +1 -0
  211. package/dist/social-providers/vk.d.mts +2 -1
  212. package/dist/social-providers/vk.mjs +2 -1
  213. package/dist/social-providers/vk.mjs.map +1 -0
  214. package/dist/social-providers/zoom.d.mts +3 -10
  215. package/dist/social-providers/zoom.mjs +2 -1
  216. package/dist/social-providers/zoom.mjs.map +1 -0
  217. package/dist/types/context.d.mts +53 -21
  218. package/dist/types/cookie.d.mts +2 -1
  219. package/dist/types/helper.d.mts +4 -1
  220. package/dist/types/index.d.mts +4 -3
  221. package/dist/types/init-options.d.mts +231 -159
  222. package/dist/types/plugin-client.d.mts +4 -1
  223. package/dist/types/plugin.d.mts +12 -11
  224. package/dist/types/secret.d.mts +12 -0
  225. package/dist/utils/db.d.mts +12 -0
  226. package/dist/utils/db.mjs +17 -0
  227. package/dist/utils/db.mjs.map +1 -0
  228. package/dist/utils/deprecate.d.mts +2 -2
  229. package/dist/utils/deprecate.mjs +2 -1
  230. package/dist/utils/deprecate.mjs.map +1 -0
  231. package/dist/utils/error-codes.d.mts +8 -6
  232. package/dist/utils/error-codes.mjs +3 -2
  233. package/dist/utils/error-codes.mjs.map +1 -0
  234. package/dist/utils/id.d.mts +2 -1
  235. package/dist/utils/id.mjs +2 -1
  236. package/dist/utils/id.mjs.map +1 -0
  237. package/dist/utils/ip.d.mts +2 -1
  238. package/dist/utils/ip.mjs +2 -1
  239. package/dist/utils/ip.mjs.map +1 -0
  240. package/dist/utils/json.d.mts +2 -1
  241. package/dist/utils/json.mjs +2 -1
  242. package/dist/utils/json.mjs.map +1 -0
  243. package/dist/utils/string.d.mts +2 -1
  244. package/dist/utils/string.mjs +2 -1
  245. package/dist/utils/string.mjs.map +1 -0
  246. package/dist/utils/url.d.mts +2 -1
  247. package/dist/utils/url.mjs +2 -1
  248. package/dist/utils/url.mjs.map +1 -0
  249. package/package.json +35 -13
  250. package/src/db/adapter/factory.ts +41 -73
  251. package/src/db/adapter/get-id-field.ts +1 -3
  252. package/src/db/adapter/index.ts +20 -15
  253. package/src/db/adapter/types.ts +2 -41
  254. package/src/db/get-tables.ts +48 -37
  255. package/src/db/index.ts +30 -5
  256. package/src/db/schema/account.ts +16 -3
  257. package/src/db/schema/rate-limit.ts +16 -1
  258. package/src/db/schema/session.ts +15 -3
  259. package/src/db/schema/user.ts +15 -3
  260. package/src/db/schema/verification.ts +16 -3
  261. package/src/db/test/get-tables.test.ts +33 -0
  262. package/src/db/type.ts +154 -1
  263. package/src/env/env-impl.ts +2 -2
  264. package/src/env/logger.ts +1 -1
  265. package/src/error/codes.ts +17 -0
  266. package/src/oauth2/client-credentials-token.ts +26 -2
  267. package/src/oauth2/create-authorization-url.ts +3 -1
  268. package/src/oauth2/index.ts +3 -0
  269. package/src/oauth2/oauth-provider.ts +1 -1
  270. package/src/oauth2/refresh-access-token.test.ts +90 -0
  271. package/src/oauth2/refresh-access-token.ts +37 -4
  272. package/src/oauth2/validate-authorization-code.ts +55 -24
  273. package/src/oauth2/validate-token.test.ts +107 -52
  274. package/src/social-providers/apple.ts +29 -29
  275. package/src/social-providers/cognito.ts +6 -5
  276. package/src/social-providers/facebook.ts +3 -3
  277. package/src/social-providers/github.ts +26 -4
  278. package/src/social-providers/gitlab.ts +1 -1
  279. package/src/social-providers/google.ts +18 -14
  280. package/src/social-providers/huggingface.ts +1 -1
  281. package/src/social-providers/index.ts +9 -5
  282. package/src/social-providers/kakao.ts +1 -1
  283. package/src/social-providers/line.ts +1 -1
  284. package/src/social-providers/microsoft-entra-id.ts +84 -1
  285. package/src/social-providers/naver.ts +1 -1
  286. package/src/social-providers/notion.ts +1 -1
  287. package/src/social-providers/paybin.ts +1 -5
  288. package/src/social-providers/polar.ts +1 -1
  289. package/src/social-providers/railway.ts +100 -0
  290. package/src/social-providers/tiktok.ts +2 -1
  291. package/src/social-providers/vercel.ts +1 -1
  292. package/src/social-providers/zoom.ts +0 -8
  293. package/src/types/context.ts +74 -14
  294. package/src/types/helper.ts +9 -0
  295. package/src/types/index.ts +14 -2
  296. package/src/types/init-options.ts +294 -186
  297. package/src/types/plugin-client.ts +1 -0
  298. package/src/types/plugin.ts +11 -6
  299. package/src/types/secret.ts +8 -0
  300. package/src/utils/db.ts +20 -0
  301. package/src/utils/deprecate.test.ts +0 -1
  302. package/src/utils/error-codes.ts +12 -9
  303. package/.turbo/turbo-build.log +0 -182
  304. package/tsconfig.json +0 -7
  305. package/tsdown.config.ts +0 -32
  306. package/vitest.config.ts +0 -3
@@ -32,16 +32,21 @@ export const getAuthTables = (
32
32
  fields: {
33
33
  key: {
34
34
  type: "string",
35
+ unique: true,
36
+ required: true,
35
37
  fieldName: options.rateLimit?.fields?.key || "key",
36
38
  },
37
39
  count: {
38
40
  type: "number",
41
+ required: true,
39
42
  fieldName: options.rateLimit?.fields?.count || "count",
40
43
  },
41
44
  lastRequest: {
42
45
  type: "number",
43
46
  bigint: true,
47
+ required: true,
44
48
  fieldName: options.rateLimit?.fields?.lastRequest || "lastRequest",
49
+ defaultValue: () => Date.now(),
45
50
  },
46
51
  },
47
52
  },
@@ -50,6 +55,46 @@ export const getAuthTables = (
50
55
  const { user, session, account, verification, ...pluginTables } =
51
56
  pluginSchema;
52
57
 
58
+ const verificationTable = {
59
+ verification: {
60
+ modelName: options.verification?.modelName || "verification",
61
+ fields: {
62
+ identifier: {
63
+ type: "string",
64
+ required: true,
65
+ fieldName: options.verification?.fields?.identifier || "identifier",
66
+ index: true,
67
+ },
68
+ value: {
69
+ type: "string",
70
+ required: true,
71
+ fieldName: options.verification?.fields?.value || "value",
72
+ },
73
+ expiresAt: {
74
+ type: "date",
75
+ required: true,
76
+ fieldName: options.verification?.fields?.expiresAt || "expiresAt",
77
+ },
78
+ createdAt: {
79
+ type: "date",
80
+ required: true,
81
+ defaultValue: () => new Date(),
82
+ fieldName: options.verification?.fields?.createdAt || "createdAt",
83
+ },
84
+ updatedAt: {
85
+ type: "date",
86
+ required: true,
87
+ defaultValue: () => new Date(),
88
+ onUpdate: () => new Date(),
89
+ fieldName: options.verification?.fields?.updatedAt || "updatedAt",
90
+ },
91
+ ...verification?.fields,
92
+ ...options.verification?.additionalFields,
93
+ },
94
+ order: 4,
95
+ },
96
+ } satisfies BetterAuthDBSchema;
97
+
53
98
  const sessionTable = {
54
99
  session: {
55
100
  modelName: options.session?.modelName || "session",
@@ -242,43 +287,9 @@ export const getAuthTables = (
242
287
  },
243
288
  order: 3,
244
289
  },
245
- verification: {
246
- modelName: options.verification?.modelName || "verification",
247
- fields: {
248
- identifier: {
249
- type: "string",
250
- required: true,
251
- fieldName: options.verification?.fields?.identifier || "identifier",
252
- index: true,
253
- },
254
- value: {
255
- type: "string",
256
- required: true,
257
- fieldName: options.verification?.fields?.value || "value",
258
- },
259
- expiresAt: {
260
- type: "date",
261
- required: true,
262
- fieldName: options.verification?.fields?.expiresAt || "expiresAt",
263
- },
264
- createdAt: {
265
- type: "date",
266
- required: true,
267
- defaultValue: () => new Date(),
268
- fieldName: options.verification?.fields?.createdAt || "createdAt",
269
- },
270
- updatedAt: {
271
- type: "date",
272
- required: true,
273
- defaultValue: () => new Date(),
274
- onUpdate: () => new Date(),
275
- fieldName: options.verification?.fields?.updatedAt || "updatedAt",
276
- },
277
- ...verification?.fields,
278
- ...options.verification?.additionalFields,
279
- },
280
- order: 4,
281
- },
290
+ ...(!options.secondaryStorage || options.verification?.storeInDatabase
291
+ ? verificationTable
292
+ : {}),
282
293
  ...pluginTables,
283
294
  ...(shouldAddRateLimitTable ? rateLimitTable : {}),
284
295
  } satisfies BetterAuthDBSchema;
package/src/db/index.ts CHANGED
@@ -1,11 +1,27 @@
1
1
  export { getAuthTables } from "./get-tables";
2
2
  export type { BetterAuthPluginDBSchema } from "./plugin";
3
- export { type Account, accountSchema } from "./schema/account";
4
- export { type RateLimit, rateLimitSchema } from "./schema/rate-limit";
5
- export { type Session, sessionSchema } from "./schema/session";
3
+ export {
4
+ type Account,
5
+ accountSchema,
6
+ type BaseAccount,
7
+ } from "./schema/account";
8
+ export {
9
+ type BaseRateLimit,
10
+ type RateLimit,
11
+ rateLimitSchema,
12
+ } from "./schema/rate-limit";
13
+ export {
14
+ type BaseSession,
15
+ type Session,
16
+ sessionSchema,
17
+ } from "./schema/session";
6
18
  export { coreSchema } from "./schema/shared";
7
- export { type User, userSchema } from "./schema/user";
8
- export { type Verification, verificationSchema } from "./schema/verification";
19
+ export { type BaseUser, type User, userSchema } from "./schema/user";
20
+ export {
21
+ type BaseVerification,
22
+ type Verification,
23
+ verificationSchema,
24
+ } from "./schema/verification";
9
25
  export type {
10
26
  BaseModelNames,
11
27
  BetterAuthDBSchema,
@@ -13,6 +29,15 @@ export type {
13
29
  DBFieldAttributeConfig,
14
30
  DBFieldType,
15
31
  DBPrimitive,
32
+ InferDBFieldInput,
33
+ InferDBFieldOutput,
34
+ InferDBFieldsFromOptions,
35
+ InferDBFieldsFromOptionsInput,
36
+ InferDBFieldsFromPlugins,
37
+ InferDBFieldsFromPluginsInput,
38
+ InferDBFieldsInput,
39
+ InferDBFieldsOutput,
40
+ InferDBValueType,
16
41
  ModelNames,
17
42
  SecondaryStorage,
18
43
  } from "./type";
@@ -1,4 +1,10 @@
1
1
  import * as z from "zod";
2
+ import type { Prettify } from "../../types";
3
+ import type { BetterAuthOptions } from "../../types/init-options";
4
+ import type {
5
+ InferDBFieldsFromOptions,
6
+ InferDBFieldsFromPlugins,
7
+ } from "../type";
2
8
  import { coreSchema } from "./shared";
3
9
 
4
10
  export const accountSchema = coreSchema.extend({
@@ -26,9 +32,16 @@ export const accountSchema = coreSchema.extend({
26
32
  password: z.string().nullish(),
27
33
  });
28
34
 
35
+ export type BaseAccount = z.infer<typeof accountSchema>;
36
+
29
37
  /**
30
38
  * Account schema type used by better-auth, note that it's possible that account could have additional fields
31
- *
32
- * todo: we should use generics to extend this type with additional fields from plugins and options in the future
33
39
  */
34
- export type Account = z.infer<typeof accountSchema>;
40
+ export type Account<
41
+ DBOptions extends BetterAuthOptions["account"] = BetterAuthOptions["account"],
42
+ Plugins extends BetterAuthOptions["plugins"] = BetterAuthOptions["plugins"],
43
+ > = Prettify<
44
+ BaseAccount &
45
+ InferDBFieldsFromOptions<DBOptions> &
46
+ InferDBFieldsFromPlugins<"account", Plugins>
47
+ >;
@@ -1,4 +1,9 @@
1
1
  import * as z from "zod";
2
+ import type { BetterAuthOptions, Prettify } from "../../types";
3
+ import type {
4
+ InferDBFieldsFromOptions,
5
+ InferDBFieldsFromPlugins,
6
+ } from "../type";
2
7
 
3
8
  export const rateLimitSchema = z.object({
4
9
  /**
@@ -15,7 +20,17 @@ export const rateLimitSchema = z.object({
15
20
  lastRequest: z.number(),
16
21
  });
17
22
 
23
+ export type BaseRateLimit = z.infer<typeof rateLimitSchema>;
24
+
18
25
  /**
19
26
  * Rate limit schema type used by better-auth for rate limiting
20
27
  */
21
- export type RateLimit = z.infer<typeof rateLimitSchema>;
28
+ export type RateLimit<
29
+ DBOptions extends
30
+ BetterAuthOptions["rateLimit"] = BetterAuthOptions["rateLimit"],
31
+ Plugins extends BetterAuthOptions["plugins"] = BetterAuthOptions["plugins"],
32
+ > = Prettify<
33
+ BaseRateLimit &
34
+ InferDBFieldsFromOptions<DBOptions> &
35
+ InferDBFieldsFromPlugins<"rateLimit", Plugins>
36
+ >;
@@ -1,4 +1,9 @@
1
1
  import * as z from "zod";
2
+ import type { BetterAuthOptions, Prettify } from "../../types";
3
+ import type {
4
+ InferDBFieldsFromOptions,
5
+ InferDBFieldsFromPlugins,
6
+ } from "../type";
2
7
  import { coreSchema } from "./shared";
3
8
 
4
9
  export const sessionSchema = coreSchema.extend({
@@ -9,9 +14,16 @@ export const sessionSchema = coreSchema.extend({
9
14
  userAgent: z.string().nullish(),
10
15
  });
11
16
 
17
+ export type BaseSession = z.infer<typeof sessionSchema>;
18
+
12
19
  /**
13
20
  * Session schema type used by better-auth, note that it's possible that session could have additional fields
14
- *
15
- * todo: we should use generics to extend this type with additional fields from plugins and options in the future
16
21
  */
17
- export type Session = z.infer<typeof sessionSchema>;
22
+ export type Session<
23
+ DBOptions extends BetterAuthOptions["session"] = BetterAuthOptions["session"],
24
+ Plugins extends BetterAuthOptions["plugins"] = BetterAuthOptions["plugins"],
25
+ > = Prettify<
26
+ z.infer<typeof sessionSchema> &
27
+ InferDBFieldsFromOptions<DBOptions> &
28
+ InferDBFieldsFromPlugins<"session", Plugins>
29
+ >;
@@ -1,4 +1,9 @@
1
1
  import * as z from "zod";
2
+ import type { BetterAuthOptions, Prettify } from "../../types";
3
+ import type {
4
+ InferDBFieldsFromOptions,
5
+ InferDBFieldsFromPlugins,
6
+ } from "../type";
2
7
  import { coreSchema } from "./shared";
3
8
 
4
9
  export const userSchema = coreSchema.extend({
@@ -8,9 +13,16 @@ export const userSchema = coreSchema.extend({
8
13
  image: z.string().nullish(),
9
14
  });
10
15
 
16
+ export type BaseUser = z.infer<typeof userSchema>;
17
+
11
18
  /**
12
19
  * User schema type used by better-auth, note that it's possible that user could have additional fields
13
- *
14
- * todo: we should use generics to extend this type with additional fields from plugins and options in the future
15
20
  */
16
- export type User = z.infer<typeof userSchema>;
21
+ export type User<
22
+ DBOptions extends BetterAuthOptions["user"] = BetterAuthOptions["user"],
23
+ Plugins extends BetterAuthOptions["plugins"] = BetterAuthOptions["plugins"],
24
+ > = Prettify<
25
+ BaseUser &
26
+ InferDBFieldsFromOptions<DBOptions> &
27
+ InferDBFieldsFromPlugins<"user", Plugins>
28
+ >;
@@ -1,4 +1,9 @@
1
1
  import * as z from "zod";
2
+ import type { BetterAuthOptions, Prettify } from "../../types";
3
+ import type {
4
+ InferDBFieldsFromOptions,
5
+ InferDBFieldsFromPlugins,
6
+ } from "../type";
2
7
  import { coreSchema } from "./shared";
3
8
 
4
9
  export const verificationSchema = coreSchema.extend({
@@ -7,9 +12,17 @@ export const verificationSchema = coreSchema.extend({
7
12
  identifier: z.string(),
8
13
  });
9
14
 
15
+ export type BaseVerification = z.infer<typeof verificationSchema>;
16
+
10
17
  /**
11
18
  * Verification schema type used by better-auth, note that it's possible that verification could have additional fields
12
- *
13
- * todo: we should use generics to extend this type with additional fields from plugins and options in the future
14
19
  */
15
- export type Verification = z.infer<typeof verificationSchema>;
20
+ export type Verification<
21
+ DBOptions extends
22
+ BetterAuthOptions["verification"] = BetterAuthOptions["verification"],
23
+ Plugins extends BetterAuthOptions["plugins"] = BetterAuthOptions["plugins"],
24
+ > = Prettify<
25
+ BaseVerification &
26
+ InferDBFieldsFromOptions<DBOptions> &
27
+ InferDBFieldsFromPlugins<"verification", Plugins>
28
+ >;
@@ -80,4 +80,37 @@ describe("getAuthTables", () => {
80
80
  expect(newField.fieldName).toBe("new_field");
81
81
  expect(newField.type).toBe("string");
82
82
  });
83
+
84
+ it("should exclude verification table when secondaryStorage is configured", () => {
85
+ const tables = getAuthTables({
86
+ secondaryStorage: {
87
+ get: async () => null,
88
+ set: async () => {},
89
+ delete: async () => {},
90
+ },
91
+ });
92
+
93
+ expect(tables.verification).toBeUndefined();
94
+ });
95
+
96
+ it("should include verification table when storeInDatabase is true", () => {
97
+ const tables = getAuthTables({
98
+ secondaryStorage: {
99
+ get: async () => null,
100
+ set: async () => {},
101
+ delete: async () => {},
102
+ },
103
+ verification: {
104
+ storeInDatabase: true,
105
+ },
106
+ });
107
+
108
+ expect(tables.verification).toBeDefined();
109
+ });
110
+
111
+ it("should include verification table when no secondaryStorage", () => {
112
+ const tables = getAuthTables({});
113
+
114
+ expect(tables.verification).toBeDefined();
115
+ });
83
116
  });
package/src/db/type.ts CHANGED
@@ -1,5 +1,10 @@
1
1
  import type { StandardSchemaV1 } from "@standard-schema/spec";
2
- import type { Awaitable, LiteralString } from "../types";
2
+ import type {
3
+ Awaitable,
4
+ BetterAuthOptions,
5
+ LiteralString,
6
+ UnionToIntersection,
7
+ } from "../types";
3
8
 
4
9
  export type BaseModelNames = "user" | "account" | "session" | "verification";
5
10
 
@@ -8,6 +13,154 @@ export type ModelNames<T extends string = LiteralString> =
8
13
  | T
9
14
  | "rate-limit";
10
15
 
16
+ export type InferDBValueType<T extends DBFieldType> = T extends "string"
17
+ ? string
18
+ : T extends "number"
19
+ ? number
20
+ : T extends "boolean"
21
+ ? boolean
22
+ : T extends "date"
23
+ ? Date
24
+ : T extends "json"
25
+ ? Record<string, any>
26
+ : T extends `${infer U}[]`
27
+ ? U extends "string"
28
+ ? string[]
29
+ : number[]
30
+ : T extends Array<any>
31
+ ? T[number]
32
+ : never;
33
+
34
+ export type InferDBFieldOutput<T extends DBFieldAttribute> =
35
+ T["returned"] extends false
36
+ ? never
37
+ : T["required"] extends false
38
+ ? InferDBValueType<T["type"]> | undefined | null
39
+ : InferDBValueType<T["type"]>;
40
+
41
+ export type InferDBFieldInput<T extends DBFieldAttribute> = InferDBValueType<
42
+ T["type"]
43
+ >;
44
+
45
+ export type InferDBFieldsInput<Field> =
46
+ Field extends Record<infer Key, DBFieldAttribute>
47
+ ? {
48
+ [key in Key as Field[key]["required"] extends false
49
+ ? never
50
+ : Field[key]["defaultValue"] extends string | number | boolean | Date
51
+ ? never
52
+ : Field[key]["input"] extends false
53
+ ? never
54
+ : key]: InferDBFieldInput<Field[key]>;
55
+ } & {
56
+ [key in Key as Field[key]["input"] extends false ? never : key]?:
57
+ | InferDBFieldInput<Field[key]>
58
+ | undefined
59
+ | null;
60
+ }
61
+ : {};
62
+
63
+ export type InferDBFieldsOutput<
64
+ Fields extends Record<string, DBFieldAttribute>,
65
+ > =
66
+ Fields extends Record<infer Key, DBFieldAttribute>
67
+ ? {
68
+ [key in Key as Fields[key]["returned"] extends false
69
+ ? never
70
+ : Fields[key]["required"] extends false
71
+ ? Fields[key]["defaultValue"] extends
72
+ | boolean
73
+ | string
74
+ | number
75
+ | Date
76
+ ? key
77
+ : never
78
+ : key]: InferDBFieldOutput<Fields[key]>;
79
+ } & {
80
+ [key in Key as Fields[key]["returned"] extends false
81
+ ? never
82
+ : Fields[key]["required"] extends false
83
+ ? Fields[key]["defaultValue"] extends
84
+ | boolean
85
+ | string
86
+ | number
87
+ | Date
88
+ ? never
89
+ : key
90
+ : never]?: InferDBFieldOutput<Fields[key]> | null;
91
+ }
92
+ : never;
93
+
94
+ export type InferDBFieldsFromOptionsInput<
95
+ DBOptions extends
96
+ | BetterAuthOptions["session"]
97
+ | BetterAuthOptions["user"]
98
+ | BetterAuthOptions["verification"]
99
+ | BetterAuthOptions["account"]
100
+ | BetterAuthOptions["rateLimit"],
101
+ > = DBOptions extends {
102
+ additionalFields: Record<string, DBFieldAttribute>;
103
+ }
104
+ ? InferDBFieldsInput<DBOptions["additionalFields"]>
105
+ : {};
106
+
107
+ export type InferDBFieldsFromOptions<
108
+ DBOptions extends
109
+ | BetterAuthOptions["session"]
110
+ | BetterAuthOptions["user"]
111
+ | BetterAuthOptions["verification"]
112
+ | BetterAuthOptions["account"]
113
+ | BetterAuthOptions["rateLimit"],
114
+ > = DBOptions extends {
115
+ additionalFields: Record<string, DBFieldAttribute>;
116
+ }
117
+ ? InferDBFieldsOutput<DBOptions["additionalFields"]>
118
+ : {};
119
+
120
+ export type InferDBFieldsFromPluginsInput<
121
+ ModelName extends string,
122
+ Plugins extends unknown[] | undefined,
123
+ > = Plugins extends []
124
+ ? {}
125
+ : Plugins extends [infer P, ...infer Rest]
126
+ ? P extends {
127
+ schema: {
128
+ [key in ModelName]: {
129
+ fields: infer Fields;
130
+ };
131
+ };
132
+ }
133
+ ? Fields extends Record<string, DBFieldAttribute>
134
+ ? UnionToIntersection<
135
+ InferDBFieldsInput<Fields> &
136
+ InferDBFieldsFromPluginsInput<ModelName, Rest>
137
+ >
138
+ : InferDBFieldsFromPluginsInput<ModelName, Rest>
139
+ : InferDBFieldsFromPluginsInput<ModelName, Rest>
140
+ : {};
141
+
142
+ export type InferDBFieldsFromPlugins<
143
+ ModelName extends string,
144
+ Plugins extends unknown[] | undefined,
145
+ > = Plugins extends []
146
+ ? {}
147
+ : Plugins extends [infer P, ...infer Rest]
148
+ ? P extends {
149
+ schema: {
150
+ [key in ModelName]: {
151
+ fields: infer Fields;
152
+ };
153
+ };
154
+ }
155
+ ? Fields extends Record<string, DBFieldAttribute>
156
+ ? UnionToIntersection<
157
+ InferDBFieldsOutput<Fields> &
158
+ InferDBFieldsFromPlugins<ModelName, Rest>
159
+ >
160
+ : InferDBFieldsFromPlugins<ModelName, Rest>
161
+ : InferDBFieldsFromPlugins<ModelName, Rest>
162
+ : {};
163
+
11
164
  export type DBFieldType =
12
165
  | "string"
13
166
  | "number"
@@ -115,10 +115,10 @@ export const ENV = Object.freeze({
115
115
  get PACKAGE_VERSION() {
116
116
  return getEnvVar("PACKAGE_VERSION", "0.0.0");
117
117
  },
118
- get BETTER_AUTH_TELEMETRY_ENDPOINT() {
118
+ get BETTER_AUTH_TELEMETRY_ENDPOINT(): string | undefined {
119
119
  return getEnvVar(
120
120
  "BETTER_AUTH_TELEMETRY_ENDPOINT",
121
- "https://telemetry.better-auth.com/v1/track",
121
+ import.meta.env.BETTER_AUTH_TELEMETRY_ENDPOINT,
122
122
  );
123
123
  },
124
124
  });
package/src/env/logger.ts CHANGED
@@ -94,7 +94,7 @@ export type InternalLogger = {
94
94
 
95
95
  export const createLogger = (options?: Logger | undefined): InternalLogger => {
96
96
  const enabled = options?.disabled !== true;
97
- const logLevel = options?.level ?? "error";
97
+ const logLevel = options?.level ?? "warn";
98
98
 
99
99
  const isDisableColorsSpecified = options?.disableColors !== undefined;
100
100
  const colorsEnabled = isDisableColorsSpecified
@@ -1,5 +1,18 @@
1
1
  import { defineErrorCodes } from "../utils/error-codes";
2
2
 
3
+ declare module "@better-auth/core" {
4
+ interface BetterAuthPluginRegistry<AuthOptions, Options> {
5
+ /**
6
+ * This plugin does not exist, do not use it in runtime.
7
+ */
8
+ "$internal:base": {
9
+ creator: () => {
10
+ $ERROR_CODES: typeof BASE_ERROR_CODES;
11
+ };
12
+ };
13
+ }
14
+ }
15
+
3
16
  export const BASE_ERROR_CODES = defineErrorCodes({
4
17
  USER_NOT_FOUND: "User not found",
5
18
  FAILED_TO_CREATE_USER: "Failed to create user",
@@ -49,6 +62,10 @@ export const BASE_ERROR_CODES = defineErrorCodes({
49
62
  ASYNC_VALIDATION_NOT_SUPPORTED: "Async validation is not supported",
50
63
  VALIDATION_ERROR: "Validation Error",
51
64
  MISSING_FIELD: "Field is required",
65
+ METHOD_NOT_ALLOWED_DEFER_SESSION_REQUIRED:
66
+ "POST method requires deferSessionRefresh to be enabled in session config",
67
+ BODY_MUST_BE_AN_OBJECT: "Body must be an object",
68
+ PASSWORD_ALREADY_SET: "User already has a password set",
52
69
  });
53
70
 
54
71
  export type APIErrorCode = keyof typeof BASE_ERROR_CODES;
@@ -1,7 +1,31 @@
1
1
  import { base64Url } from "@better-auth/utils/base64";
2
2
  import { betterFetch } from "@better-fetch/fetch";
3
+ import type { AwaitableFunction } from "../types";
3
4
  import type { OAuth2Tokens, ProviderOptions } from "./oauth-provider";
4
5
 
6
+ export async function clientCredentialsTokenRequest({
7
+ options,
8
+ scope,
9
+ authentication,
10
+ resource,
11
+ }: {
12
+ options: AwaitableFunction<ProviderOptions & { clientSecret: string }>;
13
+ scope?: string | undefined;
14
+ authentication?: ("basic" | "post") | undefined;
15
+ resource?: (string | string[]) | undefined;
16
+ }) {
17
+ options = typeof options === "function" ? await options() : options;
18
+ return createClientCredentialsTokenRequest({
19
+ options,
20
+ scope,
21
+ authentication,
22
+ resource,
23
+ });
24
+ }
25
+
26
+ /**
27
+ * @deprecated use async'd clientCredentialsTokenRequest instead
28
+ */
5
29
  export function createClientCredentialsTokenRequest({
6
30
  options,
7
31
  scope,
@@ -59,13 +83,13 @@ export async function clientCredentialsToken({
59
83
  authentication,
60
84
  resource,
61
85
  }: {
62
- options: ProviderOptions & { clientSecret: string };
86
+ options: AwaitableFunction<ProviderOptions & { clientSecret: string }>;
63
87
  tokenEndpoint: string;
64
88
  scope: string;
65
89
  authentication?: ("basic" | "post") | undefined;
66
90
  resource?: (string | string[]) | undefined;
67
91
  }): Promise<OAuth2Tokens> {
68
- const { body, headers } = createClientCredentialsTokenRequest({
92
+ const { body, headers } = await clientCredentialsTokenRequest({
69
93
  options,
70
94
  scope,
71
95
  authentication,
@@ -1,3 +1,4 @@
1
+ import type { AwaitableFunction } from "../types";
1
2
  import type { ProviderOptions } from "./index";
2
3
  import { generateCodeChallenge } from "./utils";
3
4
 
@@ -22,7 +23,7 @@ export async function createAuthorizationURL({
22
23
  scopeJoiner,
23
24
  }: {
24
25
  id: string;
25
- options: ProviderOptions;
26
+ options: AwaitableFunction<ProviderOptions>;
26
27
  redirectURI: string;
27
28
  authorizationEndpoint: string;
28
29
  state: string;
@@ -40,6 +41,7 @@ export async function createAuthorizationURL({
40
41
  additionalParams?: Record<string, string> | undefined;
41
42
  scopeJoiner?: string | undefined;
42
43
  }) {
44
+ options = typeof options === "function" ? await options() : options;
43
45
  const url = new URL(options.authorizationEndpoint || authorizationEndpoint);
44
46
  url.searchParams.set("response_type", responseType || "code");
45
47
  const primaryClientId = Array.isArray(options.clientId)
@@ -1,5 +1,6 @@
1
1
  export {
2
2
  clientCredentialsToken,
3
+ clientCredentialsTokenRequest,
3
4
  createClientCredentialsTokenRequest,
4
5
  } from "./client-credentials-token";
5
6
  export { createAuthorizationURL } from "./create-authorization-url";
@@ -12,9 +13,11 @@ export type {
12
13
  export {
13
14
  createRefreshAccessTokenRequest,
14
15
  refreshAccessToken,
16
+ refreshAccessTokenRequest,
15
17
  } from "./refresh-access-token";
16
18
  export { generateCodeChallenge, getOAuth2Tokens } from "./utils";
17
19
  export {
20
+ authorizationCodeRequest,
18
21
  createAuthorizationCodeRequest,
19
22
  validateAuthorizationCode,
20
23
  validateToken,
@@ -42,7 +42,7 @@ export interface OAuthProvider<
42
42
  redirectURI: string;
43
43
  codeVerifier?: string | undefined;
44
44
  deviceId?: string | undefined;
45
- }) => Promise<OAuth2Tokens>;
45
+ }) => Promise<OAuth2Tokens | null>;
46
46
  getUserInfo: (
47
47
  token: OAuth2Tokens & {
48
48
  /**