@depup/better-auth__core 1.6.23-depup.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (310) hide show
  1. package/LICENSE.md +20 -0
  2. package/README.md +32 -0
  3. package/changes.json +14 -0
  4. package/dist/api/index.d.mts +281 -0
  5. package/dist/api/index.mjs +92 -0
  6. package/dist/async_hooks/index.d.mts +6 -0
  7. package/dist/async_hooks/index.mjs +20 -0
  8. package/dist/async_hooks/pure.index.d.mts +6 -0
  9. package/dist/async_hooks/pure.index.mjs +33 -0
  10. package/dist/context/endpoint-context.d.mts +18 -0
  11. package/dist/context/endpoint-context.mjs +29 -0
  12. package/dist/context/global.d.mts +6 -0
  13. package/dist/context/global.mjs +36 -0
  14. package/dist/context/index.d.mts +5 -0
  15. package/dist/context/index.mjs +5 -0
  16. package/dist/context/request-state.d.mts +27 -0
  17. package/dist/context/request-state.mjs +47 -0
  18. package/dist/context/transaction.d.mts +27 -0
  19. package/dist/context/transaction.mjs +96 -0
  20. package/dist/db/adapter/factory.d.mts +17 -0
  21. package/dist/db/adapter/factory.mjs +905 -0
  22. package/dist/db/adapter/get-default-field-name.d.mts +18 -0
  23. package/dist/db/adapter/get-default-field-name.mjs +36 -0
  24. package/dist/db/adapter/get-default-model-name.d.mts +12 -0
  25. package/dist/db/adapter/get-default-model-name.mjs +30 -0
  26. package/dist/db/adapter/get-field-attributes.d.mts +26 -0
  27. package/dist/db/adapter/get-field-attributes.mjs +37 -0
  28. package/dist/db/adapter/get-field-name.d.mts +18 -0
  29. package/dist/db/adapter/get-field-name.mjs +31 -0
  30. package/dist/db/adapter/get-id-field.d.mts +36 -0
  31. package/dist/db/adapter/get-id-field.mjs +64 -0
  32. package/dist/db/adapter/get-model-name.d.mts +12 -0
  33. package/dist/db/adapter/get-model-name.mjs +21 -0
  34. package/dist/db/adapter/index.d.mts +615 -0
  35. package/dist/db/adapter/index.mjs +24 -0
  36. package/dist/db/adapter/types.d.mts +105 -0
  37. package/dist/db/adapter/utils.d.mts +7 -0
  38. package/dist/db/adapter/utils.mjs +37 -0
  39. package/dist/db/get-tables.d.mts +6 -0
  40. package/dist/db/get-tables.mjs +266 -0
  41. package/dist/db/index.d.mts +10 -0
  42. package/dist/db/index.mjs +8 -0
  43. package/dist/db/plugin.d.mts +12 -0
  44. package/dist/db/schema/account.d.mts +28 -0
  45. package/dist/db/schema/account.mjs +17 -0
  46. package/dist/db/schema/rate-limit.d.mts +18 -0
  47. package/dist/db/schema/rate-limit.mjs +9 -0
  48. package/dist/db/schema/session.d.mts +23 -0
  49. package/dist/db/schema/session.mjs +12 -0
  50. package/dist/db/schema/shared.d.mts +10 -0
  51. package/dist/db/schema/shared.mjs +9 -0
  52. package/dist/db/schema/user.d.mts +22 -0
  53. package/dist/db/schema/user.mjs +11 -0
  54. package/dist/db/schema/verification.d.mts +21 -0
  55. package/dist/db/schema/verification.mjs +10 -0
  56. package/dist/db/type.d.mts +194 -0
  57. package/dist/env/color-depth.d.mts +4 -0
  58. package/dist/env/color-depth.mjs +86 -0
  59. package/dist/env/env-impl.d.mts +32 -0
  60. package/dist/env/env-impl.mjs +81 -0
  61. package/dist/env/index.d.mts +4 -0
  62. package/dist/env/index.mjs +4 -0
  63. package/dist/env/logger.d.mts +48 -0
  64. package/dist/env/logger.mjs +79 -0
  65. package/dist/error/codes.d.mts +69 -0
  66. package/dist/error/codes.mjs +55 -0
  67. package/dist/error/index.d.mts +26 -0
  68. package/dist/error/index.mjs +27 -0
  69. package/dist/index.d.mts +9 -0
  70. package/dist/index.mjs +1 -0
  71. package/dist/instrumentation/api.mjs +12 -0
  72. package/dist/instrumentation/attributes.d.mts +11 -0
  73. package/dist/instrumentation/attributes.mjs +10 -0
  74. package/dist/instrumentation/index.d.mts +3 -0
  75. package/dist/instrumentation/index.mjs +3 -0
  76. package/dist/instrumentation/noop.mjs +42 -0
  77. package/dist/instrumentation/pure.index.d.mts +7 -0
  78. package/dist/instrumentation/pure.index.mjs +7 -0
  79. package/dist/instrumentation/tracer.d.mts +13 -0
  80. package/dist/instrumentation/tracer.mjs +53 -0
  81. package/dist/oauth2/client-credentials-token.d.mts +56 -0
  82. package/dist/oauth2/client-credentials-token.mjs +64 -0
  83. package/dist/oauth2/create-authorization-url.d.mts +44 -0
  84. package/dist/oauth2/create-authorization-url.mjs +41 -0
  85. package/dist/oauth2/index.d.mts +8 -0
  86. package/dist/oauth2/index.mjs +7 -0
  87. package/dist/oauth2/oauth-provider.d.mts +192 -0
  88. package/dist/oauth2/refresh-access-token.d.mts +54 -0
  89. package/dist/oauth2/refresh-access-token.mjs +73 -0
  90. package/dist/oauth2/reject-redirects.mjs +60 -0
  91. package/dist/oauth2/utils.d.mts +24 -0
  92. package/dist/oauth2/utils.mjs +48 -0
  93. package/dist/oauth2/validate-authorization-code.d.mts +85 -0
  94. package/dist/oauth2/validate-authorization-code.mjs +86 -0
  95. package/dist/oauth2/verify.d.mts +65 -0
  96. package/dist/oauth2/verify.mjs +202 -0
  97. package/dist/social-providers/apple.d.mts +133 -0
  98. package/dist/social-providers/apple.mjs +117 -0
  99. package/dist/social-providers/atlassian.d.mts +70 -0
  100. package/dist/social-providers/atlassian.mjs +80 -0
  101. package/dist/social-providers/cognito.d.mts +85 -0
  102. package/dist/social-providers/cognito.mjs +162 -0
  103. package/dist/social-providers/discord.d.mts +124 -0
  104. package/dist/social-providers/discord.mjs +62 -0
  105. package/dist/social-providers/dropbox.d.mts +69 -0
  106. package/dist/social-providers/dropbox.mjs +72 -0
  107. package/dist/social-providers/facebook.d.mts +79 -0
  108. package/dist/social-providers/facebook.mjs +157 -0
  109. package/dist/social-providers/figma.d.mts +61 -0
  110. package/dist/social-providers/figma.mjs +83 -0
  111. package/dist/social-providers/github.d.mts +102 -0
  112. package/dist/social-providers/github.mjs +92 -0
  113. package/dist/social-providers/gitlab.d.mts +123 -0
  114. package/dist/social-providers/gitlab.mjs +79 -0
  115. package/dist/social-providers/google.d.mts +123 -0
  116. package/dist/social-providers/google.mjs +137 -0
  117. package/dist/social-providers/huggingface.d.mts +83 -0
  118. package/dist/social-providers/huggingface.mjs +73 -0
  119. package/dist/social-providers/index.d.mts +1834 -0
  120. package/dist/social-providers/index.mjs +78 -0
  121. package/dist/social-providers/kakao.d.mts +161 -0
  122. package/dist/social-providers/kakao.mjs +70 -0
  123. package/dist/social-providers/kick.d.mts +73 -0
  124. package/dist/social-providers/kick.mjs +68 -0
  125. package/dist/social-providers/line.d.mts +105 -0
  126. package/dist/social-providers/line.mjs +110 -0
  127. package/dist/social-providers/linear.d.mts +68 -0
  128. package/dist/social-providers/linear.mjs +85 -0
  129. package/dist/social-providers/linkedin.d.mts +67 -0
  130. package/dist/social-providers/linkedin.mjs +73 -0
  131. package/dist/social-providers/microsoft-entra-id.d.mts +174 -0
  132. package/dist/social-providers/microsoft-entra-id.mjs +152 -0
  133. package/dist/social-providers/naver.d.mts +92 -0
  134. package/dist/social-providers/naver.mjs +65 -0
  135. package/dist/social-providers/notion.d.mts +64 -0
  136. package/dist/social-providers/notion.mjs +72 -0
  137. package/dist/social-providers/paybin.d.mts +71 -0
  138. package/dist/social-providers/paybin.mjs +81 -0
  139. package/dist/social-providers/paypal.d.mts +131 -0
  140. package/dist/social-providers/paypal.mjs +188 -0
  141. package/dist/social-providers/polar.d.mts +74 -0
  142. package/dist/social-providers/polar.mjs +71 -0
  143. package/dist/social-providers/railway.d.mts +65 -0
  144. package/dist/social-providers/railway.mjs +74 -0
  145. package/dist/social-providers/reddit.d.mts +62 -0
  146. package/dist/social-providers/reddit.mjs +81 -0
  147. package/dist/social-providers/roblox.d.mts +70 -0
  148. package/dist/social-providers/roblox.mjs +57 -0
  149. package/dist/social-providers/salesforce.d.mts +79 -0
  150. package/dist/social-providers/salesforce.mjs +87 -0
  151. package/dist/social-providers/slack.d.mts +83 -0
  152. package/dist/social-providers/slack.mjs +66 -0
  153. package/dist/social-providers/spotify.d.mts +63 -0
  154. package/dist/social-providers/spotify.mjs +69 -0
  155. package/dist/social-providers/tiktok.d.mts +168 -0
  156. package/dist/social-providers/tiktok.mjs +60 -0
  157. package/dist/social-providers/twitch.d.mts +79 -0
  158. package/dist/social-providers/twitch.mjs +75 -0
  159. package/dist/social-providers/twitter.d.mts +126 -0
  160. package/dist/social-providers/twitter.mjs +85 -0
  161. package/dist/social-providers/vercel.d.mts +62 -0
  162. package/dist/social-providers/vercel.mjs +58 -0
  163. package/dist/social-providers/vk.d.mts +70 -0
  164. package/dist/social-providers/vk.mjs +81 -0
  165. package/dist/social-providers/wechat.d.mts +113 -0
  166. package/dist/social-providers/wechat.mjs +81 -0
  167. package/dist/social-providers/zoom.d.mts +163 -0
  168. package/dist/social-providers/zoom.mjs +69 -0
  169. package/dist/types/context.d.mts +319 -0
  170. package/dist/types/cookie.d.mts +15 -0
  171. package/dist/types/helper.d.mts +10 -0
  172. package/dist/types/index.d.mts +9 -0
  173. package/dist/types/init-options.d.mts +1430 -0
  174. package/dist/types/plugin-client.d.mts +123 -0
  175. package/dist/types/plugin.d.mts +124 -0
  176. package/dist/types/secret.d.mts +11 -0
  177. package/dist/utils/async.d.mts +22 -0
  178. package/dist/utils/async.mjs +32 -0
  179. package/dist/utils/db.d.mts +9 -0
  180. package/dist/utils/db.mjs +15 -0
  181. package/dist/utils/deprecate.d.mts +9 -0
  182. package/dist/utils/deprecate.mjs +16 -0
  183. package/dist/utils/error-codes.d.mts +13 -0
  184. package/dist/utils/error-codes.mjs +10 -0
  185. package/dist/utils/fetch-metadata.d.mts +4 -0
  186. package/dist/utils/fetch-metadata.mjs +6 -0
  187. package/dist/utils/host.d.mts +147 -0
  188. package/dist/utils/host.mjs +295 -0
  189. package/dist/utils/id.d.mts +4 -0
  190. package/dist/utils/id.mjs +7 -0
  191. package/dist/utils/ip.d.mts +77 -0
  192. package/dist/utils/ip.mjs +230 -0
  193. package/dist/utils/is-api-error.d.mts +6 -0
  194. package/dist/utils/is-api-error.mjs +8 -0
  195. package/dist/utils/json.d.mts +4 -0
  196. package/dist/utils/json.mjs +41 -0
  197. package/dist/utils/redirect-uri.d.mts +20 -0
  198. package/dist/utils/redirect-uri.mjs +48 -0
  199. package/dist/utils/string.d.mts +8 -0
  200. package/dist/utils/string.mjs +25 -0
  201. package/dist/utils/url.d.mts +37 -0
  202. package/dist/utils/url.mjs +61 -0
  203. package/package.json +216 -0
  204. package/src/api/index.ts +185 -0
  205. package/src/async_hooks/index.ts +40 -0
  206. package/src/async_hooks/pure.index.ts +46 -0
  207. package/src/context/endpoint-context.ts +50 -0
  208. package/src/context/global.ts +57 -0
  209. package/src/context/index.ts +23 -0
  210. package/src/context/request-state.ts +91 -0
  211. package/src/context/transaction.ts +169 -0
  212. package/src/db/adapter/factory.ts +1740 -0
  213. package/src/db/adapter/get-default-field-name.ts +59 -0
  214. package/src/db/adapter/get-default-model-name.ts +51 -0
  215. package/src/db/adapter/get-field-attributes.ts +62 -0
  216. package/src/db/adapter/get-field-name.ts +43 -0
  217. package/src/db/adapter/get-id-field.ts +150 -0
  218. package/src/db/adapter/get-model-name.ts +36 -0
  219. package/src/db/adapter/index.ts +656 -0
  220. package/src/db/adapter/types.ts +134 -0
  221. package/src/db/adapter/utils.ts +61 -0
  222. package/src/db/get-tables.ts +304 -0
  223. package/src/db/index.ts +43 -0
  224. package/src/db/plugin.ts +11 -0
  225. package/src/db/schema/account.ts +47 -0
  226. package/src/db/schema/rate-limit.ts +36 -0
  227. package/src/db/schema/session.ts +29 -0
  228. package/src/db/schema/shared.ts +7 -0
  229. package/src/db/schema/user.ts +31 -0
  230. package/src/db/schema/verification.ts +28 -0
  231. package/src/db/type.ts +360 -0
  232. package/src/env/color-depth.ts +172 -0
  233. package/src/env/env-impl.ts +123 -0
  234. package/src/env/index.ts +23 -0
  235. package/src/env/logger.ts +145 -0
  236. package/src/error/codes.ts +72 -0
  237. package/src/error/index.ts +44 -0
  238. package/src/index.ts +1 -0
  239. package/src/instrumentation/api.ts +17 -0
  240. package/src/instrumentation/attributes.ts +22 -0
  241. package/src/instrumentation/index.ts +2 -0
  242. package/src/instrumentation/noop.ts +74 -0
  243. package/src/instrumentation/pure.index.ts +31 -0
  244. package/src/instrumentation/tracer.ts +95 -0
  245. package/src/oauth2/client-credentials-token.ts +126 -0
  246. package/src/oauth2/create-authorization-url.ts +89 -0
  247. package/src/oauth2/index.ts +34 -0
  248. package/src/oauth2/oauth-provider.ts +222 -0
  249. package/src/oauth2/refresh-access-token.ts +157 -0
  250. package/src/oauth2/reject-redirects.ts +70 -0
  251. package/src/oauth2/utils.ts +70 -0
  252. package/src/oauth2/validate-authorization-code.ts +190 -0
  253. package/src/oauth2/verify.ts +408 -0
  254. package/src/social-providers/apple.ts +249 -0
  255. package/src/social-providers/atlassian.ts +133 -0
  256. package/src/social-providers/cognito.ts +281 -0
  257. package/src/social-providers/discord.ts +170 -0
  258. package/src/social-providers/dropbox.ts +112 -0
  259. package/src/social-providers/facebook.ts +288 -0
  260. package/src/social-providers/figma.ts +118 -0
  261. package/src/social-providers/github.ts +184 -0
  262. package/src/social-providers/gitlab.ts +155 -0
  263. package/src/social-providers/google.ts +263 -0
  264. package/src/social-providers/huggingface.ts +119 -0
  265. package/src/social-providers/index.ts +132 -0
  266. package/src/social-providers/kakao.ts +179 -0
  267. package/src/social-providers/kick.ts +109 -0
  268. package/src/social-providers/line.ts +169 -0
  269. package/src/social-providers/linear.ts +121 -0
  270. package/src/social-providers/linkedin.ts +110 -0
  271. package/src/social-providers/microsoft-entra-id.ts +391 -0
  272. package/src/social-providers/naver.ts +113 -0
  273. package/src/social-providers/notion.ts +108 -0
  274. package/src/social-providers/paybin.ts +118 -0
  275. package/src/social-providers/paypal.ts +371 -0
  276. package/src/social-providers/polar.ts +111 -0
  277. package/src/social-providers/railway.ts +100 -0
  278. package/src/social-providers/reddit.ts +126 -0
  279. package/src/social-providers/roblox.ts +112 -0
  280. package/src/social-providers/salesforce.ts +159 -0
  281. package/src/social-providers/slack.ts +112 -0
  282. package/src/social-providers/spotify.ts +94 -0
  283. package/src/social-providers/tiktok.ts +211 -0
  284. package/src/social-providers/twitch.ts +112 -0
  285. package/src/social-providers/twitter.ts +199 -0
  286. package/src/social-providers/vercel.ts +87 -0
  287. package/src/social-providers/vk.ts +125 -0
  288. package/src/social-providers/wechat.ts +220 -0
  289. package/src/social-providers/zoom.ts +230 -0
  290. package/src/types/context.ts +460 -0
  291. package/src/types/cookie.ts +10 -0
  292. package/src/types/helper.ts +27 -0
  293. package/src/types/index.ts +40 -0
  294. package/src/types/init-options.ts +1679 -0
  295. package/src/types/plugin-client.ts +143 -0
  296. package/src/types/plugin.ts +163 -0
  297. package/src/types/secret.ts +8 -0
  298. package/src/utils/async.ts +53 -0
  299. package/src/utils/db.ts +20 -0
  300. package/src/utils/deprecate.ts +21 -0
  301. package/src/utils/error-codes.ts +68 -0
  302. package/src/utils/fetch-metadata.ts +3 -0
  303. package/src/utils/host.ts +416 -0
  304. package/src/utils/id.ts +5 -0
  305. package/src/utils/ip.ts +395 -0
  306. package/src/utils/is-api-error.ts +10 -0
  307. package/src/utils/json.ts +56 -0
  308. package/src/utils/redirect-uri.ts +54 -0
  309. package/src/utils/string.ts +40 -0
  310. package/src/utils/url.ts +77 -0
@@ -0,0 +1,31 @@
1
+ import * as z from "zod";
2
+ import type { BetterAuthOptions, Prettify } from "../../types";
3
+ import type {
4
+ InferDBFieldsFromOptions,
5
+ InferDBFieldsFromPlugins,
6
+ } from "../type";
7
+ import { coreSchema } from "./shared";
8
+
9
+ export const userSchema = coreSchema.extend({
10
+ // TODO(#9124): widen to nullish in v2. OAuth providers (Discord phone-only,
11
+ // Apple subsequent sign-ins, etc.) can legitimately omit email; identity
12
+ // must key on (providerId, accountId) per OpenID Connect Core §5.7.
13
+ email: z.string().transform((val) => val.toLowerCase()),
14
+ emailVerified: z.boolean().default(false),
15
+ name: z.string(),
16
+ image: z.string().nullish(),
17
+ });
18
+
19
+ export type BaseUser = z.infer<typeof userSchema>;
20
+
21
+ /**
22
+ * User schema type used by better-auth, note that it's possible that user could have additional fields
23
+ */
24
+ export type User<
25
+ DBOptions extends BetterAuthOptions["user"] = BetterAuthOptions["user"],
26
+ Plugins extends BetterAuthOptions["plugins"] = BetterAuthOptions["plugins"],
27
+ > = Prettify<
28
+ BaseUser &
29
+ InferDBFieldsFromOptions<DBOptions> &
30
+ InferDBFieldsFromPlugins<"user", Plugins>
31
+ >;
@@ -0,0 +1,28 @@
1
+ import * as z from "zod";
2
+ import type { BetterAuthOptions, Prettify } from "../../types";
3
+ import type {
4
+ InferDBFieldsFromOptions,
5
+ InferDBFieldsFromPlugins,
6
+ } from "../type";
7
+ import { coreSchema } from "./shared";
8
+
9
+ export const verificationSchema = coreSchema.extend({
10
+ value: z.string(),
11
+ expiresAt: z.date(),
12
+ identifier: z.string(),
13
+ });
14
+
15
+ export type BaseVerification = z.infer<typeof verificationSchema>;
16
+
17
+ /**
18
+ * Verification schema type used by better-auth, note that it's possible that verification could have additional fields
19
+ */
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
+ >;
package/src/db/type.ts ADDED
@@ -0,0 +1,360 @@
1
+ import type { StandardSchemaV1 } from "@standard-schema/spec";
2
+ import type {
3
+ Awaitable,
4
+ BetterAuthOptions,
5
+ LiteralString,
6
+ UnionToIntersection,
7
+ } from "../types";
8
+
9
+ export type BaseModelNames = "user" | "account" | "session" | "verification";
10
+
11
+ export type ModelNames<T extends string = LiteralString> =
12
+ | BaseModelNames
13
+ | T
14
+ | "rate-limit";
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
+
164
+ export type DBFieldType =
165
+ | "string"
166
+ | "number"
167
+ | "boolean"
168
+ | "date"
169
+ | "json"
170
+ | `${"string" | "number"}[]`
171
+ | Array<LiteralString>;
172
+
173
+ export type DBPrimitive =
174
+ | string
175
+ | number
176
+ | boolean
177
+ | Date
178
+ | null
179
+ | undefined
180
+ | string[]
181
+ | number[]
182
+ | (Record<string, unknown> | unknown[]);
183
+
184
+ export type DBFieldAttributeConfig = {
185
+ /**
186
+ * If the field should be required on a new record.
187
+ * @default true
188
+ */
189
+ required?: boolean | undefined;
190
+ /**
191
+ * If the value should be returned on a response body.
192
+ * @default true
193
+ */
194
+ returned?: boolean | undefined;
195
+ /**
196
+ * If a value should be provided when creating a new record.
197
+ * @default true
198
+ */
199
+ input?: boolean | undefined;
200
+ /**
201
+ * Default value for the field
202
+ *
203
+ * Note: This will not create a default value on the database level. It will only
204
+ * be used when creating a new record.
205
+ */
206
+ defaultValue?: (DBPrimitive | (() => DBPrimitive)) | undefined;
207
+ /**
208
+ * Update value for the field
209
+ *
210
+ * Note: This will create an onUpdate trigger on the database level for supported adapters.
211
+ * It will be called when updating a record.
212
+ */
213
+ onUpdate?: (() => DBPrimitive) | undefined;
214
+ /**
215
+ * transform the value before storing it.
216
+ */
217
+ transform?:
218
+ | {
219
+ input?: (value: DBPrimitive) => Awaitable<DBPrimitive>;
220
+ output?: (value: DBPrimitive) => Awaitable<DBPrimitive>;
221
+ }
222
+ | undefined;
223
+ /**
224
+ * Reference to another model.
225
+ */
226
+ references?:
227
+ | {
228
+ /**
229
+ * The model to reference.
230
+ */
231
+ model: string;
232
+ /**
233
+ * The field on the referenced model.
234
+ */
235
+ field: string;
236
+ /**
237
+ * The action to perform when the reference is deleted.
238
+ * @default "cascade"
239
+ */
240
+ onDelete?:
241
+ | "no action"
242
+ | "restrict"
243
+ | "cascade"
244
+ | "set null"
245
+ | "set default";
246
+ }
247
+ | undefined;
248
+ unique?: boolean | undefined;
249
+ /**
250
+ * If the field should be a bigint on the database instead of integer.
251
+ */
252
+ bigint?: boolean | undefined;
253
+ /**
254
+ * A zod schema to validate the value.
255
+ */
256
+ validator?:
257
+ | {
258
+ input?: StandardSchemaV1;
259
+ output?: StandardSchemaV1;
260
+ }
261
+ | undefined;
262
+ /**
263
+ * The name of the field on the database.
264
+ */
265
+ fieldName?: string | undefined;
266
+ /**
267
+ * If the field should be sortable.
268
+ *
269
+ * applicable only for `text` type.
270
+ * It's useful to mark fields varchar instead of text.
271
+ */
272
+ sortable?: boolean | undefined;
273
+ /**
274
+ * If the field should be indexed.
275
+ * @default false
276
+ */
277
+ index?: boolean | undefined;
278
+ };
279
+
280
+ export type DBFieldAttribute<T extends DBFieldType = DBFieldType> = {
281
+ type: T;
282
+ } & DBFieldAttributeConfig;
283
+
284
+ export type BetterAuthDBSchema = Record<
285
+ string,
286
+ {
287
+ /**
288
+ * The name of the table in the database
289
+ */
290
+ modelName: string;
291
+ /**
292
+ * The fields of the table
293
+ */
294
+ fields: Record<string, DBFieldAttribute>;
295
+ /**
296
+ * Whether to disable migrations for this table
297
+ * @default false
298
+ */
299
+ disableMigrations?: boolean | undefined;
300
+ /**
301
+ * The order of the table
302
+ */
303
+ order?: number | undefined;
304
+ }
305
+ >;
306
+
307
+ export interface SecondaryStorage {
308
+ /**
309
+ *
310
+ * @param key - Key to get
311
+ * @returns - Value of the key
312
+ */
313
+ get: (key: string) => Awaitable<unknown>;
314
+ /**
315
+ * Atomically get a value and delete it from storage.
316
+ *
317
+ * This is optional for backwards compatibility with existing secondary
318
+ * storage implementations. Single-use credential consumers use it when
319
+ * present to avoid a read-then-delete race.
320
+ *
321
+ * TODO(secondary-storage-atomic-consume): make this required in the next
322
+ * breaking release, or require database-backed verification storage for
323
+ * security-sensitive consume paths.
324
+ */
325
+ getAndDelete?: (key: string) => Awaitable<unknown>;
326
+ /**
327
+ * Atomically increment the counter at `key` by one, returning the
328
+ * post-increment value.
329
+ *
330
+ * When the key is absent, it is created with a value of `1` and the given
331
+ * `ttl` (in SECONDS). The TTL is applied only on creation; later increments
332
+ * never extend it, so the counter expires a fixed window after it was first
333
+ * created.
334
+ *
335
+ * This is optional for backwards compatibility with existing secondary
336
+ * storage implementations. TODO(secondary-storage-increment-required): make
337
+ * this required for secondary-storage-backed rate limiting in the next minor
338
+ * on `next`.
339
+ */
340
+ increment?: (key: string, ttl: number) => Awaitable<number>;
341
+ set: (
342
+ /**
343
+ * Key to store
344
+ */
345
+ key: string,
346
+ /**
347
+ * Value to store
348
+ */
349
+ value: string,
350
+ /**
351
+ * Time to live in seconds
352
+ */
353
+ ttl?: number | undefined,
354
+ ) => Awaitable<void | null | unknown>;
355
+ /**
356
+ *
357
+ * @param key - Key to delete
358
+ */
359
+ delete: (key: string) => Awaitable<void | null | string>;
360
+ }
@@ -0,0 +1,172 @@
1
+ // Source code copied & modified from node internals: https://github.com/nodejs/node/blob/5b32bb1573dace2dd058c05ac4fab1e4e446c775/lib/internal/tty.js#L123
2
+ import { env, getEnvVar } from "./env-impl";
3
+
4
+ const COLORS_2 = 1;
5
+ const COLORS_16 = 4;
6
+ const COLORS_256 = 8;
7
+ const COLORS_16m = 24;
8
+
9
+ const TERM_ENVS: Record<string, number> = {
10
+ eterm: COLORS_16,
11
+ cons25: COLORS_16,
12
+ console: COLORS_16,
13
+ cygwin: COLORS_16,
14
+ dtterm: COLORS_16,
15
+ gnome: COLORS_16,
16
+ hurd: COLORS_16,
17
+ jfbterm: COLORS_16,
18
+ konsole: COLORS_16,
19
+ kterm: COLORS_16,
20
+ mlterm: COLORS_16,
21
+ mosh: COLORS_16m,
22
+ putty: COLORS_16,
23
+ st: COLORS_16,
24
+ // http://lists.schmorp.de/pipermail/rxvt-unicode/2016q2/002261.html
25
+ "rxvt-unicode-24bit": COLORS_16m,
26
+ // https://bugs.launchpad.net/terminator/+bug/1030562
27
+ terminator: COLORS_16m,
28
+ "xterm-kitty": COLORS_16m,
29
+ };
30
+
31
+ const CI_ENVS_MAP = new Map(
32
+ Object.entries({
33
+ APPVEYOR: COLORS_256,
34
+ BUILDKITE: COLORS_256,
35
+ CIRCLECI: COLORS_16m,
36
+ DRONE: COLORS_256,
37
+ GITEA_ACTIONS: COLORS_16m,
38
+ GITHUB_ACTIONS: COLORS_16m,
39
+ GITLAB_CI: COLORS_256,
40
+ TRAVIS: COLORS_256,
41
+ }),
42
+ );
43
+
44
+ const TERM_ENVS_REG_EXP = [
45
+ /ansi/,
46
+ /color/,
47
+ /linux/,
48
+ /direct/,
49
+ /^con[0-9]*x[0-9]/,
50
+ /^rxvt/,
51
+ /^screen/,
52
+ /^xterm/,
53
+ /^vt100/,
54
+ /^vt220/,
55
+ ];
56
+
57
+ // The `getColorDepth` API got inspired by multiple sources such as
58
+ // https://github.com/chalk/supports-color,
59
+ // https://github.com/isaacs/color-support.
60
+ export function getColorDepth(): number {
61
+ // Use level 0-3 to support the same levels as `chalk` does. This is done for
62
+ // consistency throughout the ecosystem.
63
+ if (getEnvVar("FORCE_COLOR") !== undefined) {
64
+ switch (getEnvVar("FORCE_COLOR")) {
65
+ case "":
66
+ case "1":
67
+ case "true":
68
+ return COLORS_16;
69
+ case "2":
70
+ return COLORS_256;
71
+ case "3":
72
+ return COLORS_16m;
73
+ default:
74
+ return COLORS_2;
75
+ }
76
+ }
77
+
78
+ if (
79
+ (getEnvVar("NODE_DISABLE_COLORS") !== undefined &&
80
+ getEnvVar("NODE_DISABLE_COLORS") !== "") ||
81
+ // See https://no-color.org/
82
+ (getEnvVar("NO_COLOR") !== undefined && getEnvVar("NO_COLOR") !== "") ||
83
+ // The "dumb" special terminal, as defined by terminfo, doesn't support
84
+ // ANSI color control codes.
85
+ // See https://invisible-island.net/ncurses/terminfo.ti.html#toc-_Specials
86
+ getEnvVar("TERM") === "dumb"
87
+ ) {
88
+ return COLORS_2;
89
+ }
90
+
91
+ // Edge runtime doesn't support `process?.platform` syntax
92
+ // if (typeof process !== "undefined" && process?.platform === "win32") {
93
+ // // Windows 10 build 14931 (from 2016) has true color support
94
+ // return COLORS_16m;
95
+ // }
96
+
97
+ if (getEnvVar("TMUX")) {
98
+ return COLORS_16m;
99
+ }
100
+
101
+ // Azure DevOps
102
+ if ("TF_BUILD" in env && "AGENT_NAME" in env) {
103
+ return COLORS_16;
104
+ }
105
+
106
+ if ("CI" in env) {
107
+ for (const { 0: envName, 1: colors } of CI_ENVS_MAP) {
108
+ if (envName in env) {
109
+ return colors;
110
+ }
111
+ }
112
+ if (getEnvVar("CI_NAME") === "codeship") {
113
+ return COLORS_256;
114
+ }
115
+ return COLORS_2;
116
+ }
117
+
118
+ if ("TEAMCITY_VERSION" in env) {
119
+ return /^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.exec(
120
+ getEnvVar("TEAMCITY_VERSION"),
121
+ ) !== null
122
+ ? COLORS_16
123
+ : COLORS_2;
124
+ }
125
+
126
+ switch (getEnvVar("TERM_PROGRAM")) {
127
+ case "iTerm.app":
128
+ if (
129
+ !getEnvVar("TERM_PROGRAM_VERSION") ||
130
+ /^[0-2]\./.exec(getEnvVar("TERM_PROGRAM_VERSION")) !== null
131
+ ) {
132
+ return COLORS_256;
133
+ }
134
+ return COLORS_16m;
135
+ case "HyperTerm":
136
+ case "MacTerm":
137
+ return COLORS_16m;
138
+ case "Apple_Terminal":
139
+ return COLORS_256;
140
+ }
141
+
142
+ if (
143
+ getEnvVar("COLORTERM") === "truecolor" ||
144
+ getEnvVar("COLORTERM") === "24bit"
145
+ ) {
146
+ return COLORS_16m;
147
+ }
148
+
149
+ if (getEnvVar("TERM")) {
150
+ if (/truecolor/.exec(getEnvVar("TERM")) !== null) {
151
+ return COLORS_16m;
152
+ }
153
+
154
+ if (/^xterm-256/.exec(getEnvVar("TERM")) !== null) {
155
+ return COLORS_256;
156
+ }
157
+
158
+ const termEnv = getEnvVar("TERM").toLowerCase();
159
+
160
+ if (TERM_ENVS[termEnv]) {
161
+ return TERM_ENVS[termEnv];
162
+ }
163
+ if (TERM_ENVS_REG_EXP.some((term) => term.exec(termEnv) !== null)) {
164
+ return COLORS_16;
165
+ }
166
+ }
167
+ // Move 16 color COLORTERM below 16m and 256
168
+ if (getEnvVar("COLORTERM")) {
169
+ return COLORS_16;
170
+ }
171
+ return COLORS_2;
172
+ }
@@ -0,0 +1,123 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="bun" />
3
+ //https://github.com/unjs/std-env/blob/main/src/env.ts
4
+
5
+ const _envShim = Object.create(null);
6
+
7
+ export type EnvObject = Record<string, string | undefined>;
8
+
9
+ const _getEnv = (useShim?: boolean) =>
10
+ globalThis.process?.env ||
11
+ //@ts-expect-error
12
+ globalThis.Deno?.env.toObject() ||
13
+ //@ts-expect-error
14
+ globalThis.__env__ ||
15
+ (useShim ? _envShim : globalThis);
16
+
17
+ export const env = new Proxy<EnvObject>(_envShim, {
18
+ get(_, prop) {
19
+ const env = _getEnv();
20
+ return env[prop as any] ?? _envShim[prop];
21
+ },
22
+ has(_, prop) {
23
+ const env = _getEnv();
24
+ return prop in env || prop in _envShim;
25
+ },
26
+ set(_, prop, value) {
27
+ const env = _getEnv(true);
28
+ env[prop as any] = value;
29
+ return true;
30
+ },
31
+ deleteProperty(_, prop) {
32
+ if (!prop) {
33
+ return false;
34
+ }
35
+ const env = _getEnv(true);
36
+ delete env[prop as any];
37
+ return true;
38
+ },
39
+ ownKeys() {
40
+ const env = _getEnv(true);
41
+ return Object.keys(env);
42
+ },
43
+ });
44
+
45
+ function toBoolean(val: boolean | string | undefined) {
46
+ return val ? val !== "false" : false;
47
+ }
48
+
49
+ export const nodeENV = env.NODE_ENV ?? "";
50
+
51
+ /** Detect if `NODE_ENV` environment variable is `production` */
52
+ export const isProduction = nodeENV === "production";
53
+
54
+ /** Detect if `NODE_ENV` environment variable is `dev` or `development` */
55
+ export const isDevelopment = () =>
56
+ nodeENV === "dev" || nodeENV === "development";
57
+
58
+ /** Detect if `NODE_ENV` environment variable is `test` */
59
+ export const isTest = () => nodeENV === "test" || toBoolean(env.TEST);
60
+
61
+ /**
62
+ * Get environment variable with fallback
63
+ */
64
+ export function getEnvVar<Fallback extends string>(
65
+ key: string,
66
+ fallback?: Fallback,
67
+ ): Fallback extends string ? string : string | undefined {
68
+ if (typeof process !== "undefined" && process.env) {
69
+ return process.env[key] ?? (fallback as any);
70
+ }
71
+
72
+ // @ts-expect-error deno
73
+ if (typeof Deno !== "undefined") {
74
+ // @ts-expect-error deno
75
+ return Deno.env.get(key) ?? (fallback as string);
76
+ }
77
+
78
+ // Handle Bun
79
+ if (typeof Bun !== "undefined") {
80
+ return Bun.env[key] ?? (fallback as string);
81
+ }
82
+
83
+ return fallback as any;
84
+ }
85
+
86
+ /**
87
+ * Get boolean environment variable
88
+ */
89
+ export function getBooleanEnvVar(key: string, fallback = true): boolean {
90
+ const value = getEnvVar(key);
91
+ if (!value) return fallback;
92
+ return value !== "0" && value.toLowerCase() !== "false" && value !== "";
93
+ }
94
+
95
+ /**
96
+ * Common environment variables used in Better Auth
97
+ */
98
+ export const ENV = Object.freeze({
99
+ get BETTER_AUTH_SECRET() {
100
+ return getEnvVar("BETTER_AUTH_SECRET");
101
+ },
102
+ get AUTH_SECRET() {
103
+ return getEnvVar("AUTH_SECRET");
104
+ },
105
+ get BETTER_AUTH_TELEMETRY() {
106
+ return getEnvVar("BETTER_AUTH_TELEMETRY");
107
+ },
108
+ get BETTER_AUTH_TELEMETRY_ID() {
109
+ return getEnvVar("BETTER_AUTH_TELEMETRY_ID");
110
+ },
111
+ get NODE_ENV() {
112
+ return getEnvVar("NODE_ENV", "development");
113
+ },
114
+ get PACKAGE_VERSION() {
115
+ return getEnvVar("PACKAGE_VERSION", "0.0.0");
116
+ },
117
+ get BETTER_AUTH_TELEMETRY_ENDPOINT(): string | undefined {
118
+ return getEnvVar(
119
+ "BETTER_AUTH_TELEMETRY_ENDPOINT",
120
+ import.meta.env.BETTER_AUTH_TELEMETRY_ENDPOINT,
121
+ );
122
+ },
123
+ });