better-auth 1.6.20 → 1.6.22

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 (45) hide show
  1. package/dist/api/index.d.mts +3 -3
  2. package/dist/api/index.mjs +5 -5
  3. package/dist/api/rate-limiter/index.mjs +2 -3
  4. package/dist/api/routes/index.d.mts +1 -1
  5. package/dist/api/routes/session.d.mts +30 -3
  6. package/dist/api/routes/session.mjs +15 -4
  7. package/dist/context/create-context.mjs +6 -0
  8. package/dist/db/get-migration.mjs +1 -0
  9. package/dist/db/get-schema.d.mts +1 -0
  10. package/dist/db/get-schema.mjs +3 -1
  11. package/dist/db/index.d.mts +3 -2
  12. package/dist/db/index.mjs +5 -2
  13. package/dist/db/internal-adapter.mjs +1 -1
  14. package/dist/db/revoke-unproven-account-access.d.mts +19 -0
  15. package/dist/db/revoke-unproven-account-access.mjs +23 -0
  16. package/dist/db/schema.d.mts +2 -1
  17. package/dist/db/schema.mjs +13 -1
  18. package/dist/oauth2/link-account.mjs +20 -7
  19. package/dist/package.mjs +1 -1
  20. package/dist/plugins/admin/routes.mjs +4 -4
  21. package/dist/plugins/captcha/index.mjs +1 -1
  22. package/dist/plugins/device-authorization/index.d.mts +9 -9
  23. package/dist/plugins/device-authorization/index.mjs +1 -1
  24. package/dist/plugins/email-otp/routes.mjs +5 -1
  25. package/dist/plugins/magic-link/index.d.mts +3 -3
  26. package/dist/plugins/magic-link/index.mjs +6 -2
  27. package/dist/plugins/oauth-proxy/index.mjs +11 -6
  28. package/dist/plugins/one-tap/index.mjs +10 -9
  29. package/dist/plugins/siwe/index.mjs +5 -1
  30. package/dist/plugins/two-factor/backup-codes/index.mjs +21 -7
  31. package/dist/plugins/two-factor/client.d.mts +1 -0
  32. package/dist/plugins/two-factor/error-code.d.mts +1 -0
  33. package/dist/plugins/two-factor/error-code.mjs +1 -0
  34. package/dist/plugins/two-factor/index.d.mts +14 -0
  35. package/dist/plugins/two-factor/index.mjs +7 -1
  36. package/dist/plugins/two-factor/otp/index.mjs +17 -1
  37. package/dist/plugins/two-factor/schema.d.mts +13 -0
  38. package/dist/plugins/two-factor/schema.mjs +13 -0
  39. package/dist/plugins/two-factor/totp/index.mjs +23 -9
  40. package/dist/plugins/two-factor/types.d.mts +25 -0
  41. package/dist/plugins/two-factor/verify-two-factor.mjs +110 -3
  42. package/dist/plugins/username/index.mjs +56 -42
  43. package/package.json +9 -9
  44. package/dist/utils/get-request-ip.d.mts +0 -6
  45. package/dist/utils/get-request-ip.mjs +0 -20
@@ -32,11 +32,17 @@ const username = (options) => {
32
32
  const maxUsernameLength = options?.maxUsernameLength || 30;
33
33
  const validator = options?.usernameValidator || defaultUsernameValidator;
34
34
  const pathsWithHttpHookValidation = ["/sign-up/email", "/update-user"];
35
+ const getUsernameToValidate = (username) => options?.validationOrder?.username === "post-normalization" ? normalizer(username) : username;
36
+ async function validateUsernameValue(username) {
37
+ const usernameToValidate = getUsernameToValidate(username);
38
+ if (usernameToValidate.length < minUsernameLength) return USERNAME_ERROR_CODES.USERNAME_TOO_SHORT;
39
+ if (usernameToValidate.length > maxUsernameLength) return USERNAME_ERROR_CODES.USERNAME_TOO_LONG;
40
+ if (!await validator(usernameToValidate)) return USERNAME_ERROR_CODES.INVALID_USERNAME;
41
+ return null;
42
+ }
35
43
  async function validateUsername(username, displayUsername, adapter, currentUserId) {
36
- const usernameToValidate = options?.validationOrder?.username === "post-normalization" ? normalizer(username) : username;
37
- if (usernameToValidate.length < minUsernameLength) throw APIError.from("BAD_REQUEST", USERNAME_ERROR_CODES.USERNAME_TOO_SHORT);
38
- if (usernameToValidate.length > maxUsernameLength) throw APIError.from("BAD_REQUEST", USERNAME_ERROR_CODES.USERNAME_TOO_LONG);
39
- if (!await validator(usernameToValidate)) throw APIError.from("BAD_REQUEST", USERNAME_ERROR_CODES.INVALID_USERNAME);
44
+ const validationError = await validateUsernameValue(username);
45
+ if (validationError) throw APIError.from("BAD_REQUEST", validationError);
40
46
  const normalizedUsername = normalizer(username);
41
47
  const existingUser = await adapter.findOne({
42
48
  model: "user",
@@ -247,48 +253,56 @@ const username = (options) => {
247
253
  username: normalizer,
248
254
  displayUsername: displayUsernameNormalizer
249
255
  }), options?.schema),
250
- hooks: { before: [{
251
- matcher(context) {
252
- return context.path === "/sign-up/email" || context.path === "/update-user";
256
+ hooks: { before: [
257
+ {
258
+ matcher(context) {
259
+ return context.path === "/sign-up/email";
260
+ },
261
+ handler: createAuthMiddleware(async (ctx) => {
262
+ if (typeof ctx.body.displayUsername !== "string" || ctx.body.username !== void 0) return;
263
+ if (!await validateUsernameValue(ctx.body.displayUsername)) ctx.body.username = ctx.body.displayUsername;
264
+ })
253
265
  },
254
- handler: createAuthMiddleware(async (ctx) => {
255
- const username = typeof ctx.body.username === "string" && options?.validationOrder?.username === "post-normalization" ? normalizer(ctx.body.username) : ctx.body.username;
256
- if (username !== void 0 && typeof username === "string") {
257
- const minUsernameLength = options?.minUsernameLength || 3;
258
- const maxUsernameLength = options?.maxUsernameLength || 30;
259
- if (username.length < minUsernameLength) throw APIError.from("BAD_REQUEST", USERNAME_ERROR_CODES.USERNAME_TOO_SHORT);
260
- if (username.length > maxUsernameLength) throw APIError.from("BAD_REQUEST", USERNAME_ERROR_CODES.USERNAME_TOO_LONG);
261
- if (!await (options?.usernameValidator || defaultUsernameValidator)(username)) throw APIError.from("BAD_REQUEST", USERNAME_ERROR_CODES.INVALID_USERNAME);
262
- const normalizedUsername = normalizer(ctx.body.username);
263
- const existingUser = await ctx.context.adapter.findOne({
264
- model: "user",
265
- where: [{
266
- field: "username",
267
- value: normalizedUsername
268
- }]
269
- });
270
- if (ctx.path === "/sign-up/email" && existingUser) throw APIError.from("BAD_REQUEST", USERNAME_ERROR_CODES.USERNAME_IS_ALREADY_TAKEN);
271
- if (ctx.path === "/update-user" && existingUser) {
272
- const session = await getSessionFromCtx(ctx);
273
- if (!session || existingUser.id !== session.user.id) throw APIError.from("BAD_REQUEST", USERNAME_ERROR_CODES.USERNAME_IS_ALREADY_TAKEN);
266
+ {
267
+ matcher(context) {
268
+ return context.path === "/sign-up/email" || context.path === "/update-user";
269
+ },
270
+ handler: createAuthMiddleware(async (ctx) => {
271
+ const username = ctx.body.username;
272
+ if (username !== void 0 && typeof username === "string") {
273
+ const validationError = await validateUsernameValue(username);
274
+ if (validationError) throw APIError.from("BAD_REQUEST", validationError);
275
+ const normalizedUsername = normalizer(username);
276
+ const existingUser = await ctx.context.adapter.findOne({
277
+ model: "user",
278
+ where: [{
279
+ field: "username",
280
+ value: normalizedUsername
281
+ }]
282
+ });
283
+ if (ctx.path === "/sign-up/email" && existingUser) throw APIError.from("BAD_REQUEST", USERNAME_ERROR_CODES.USERNAME_IS_ALREADY_TAKEN);
284
+ if (ctx.path === "/update-user" && existingUser) {
285
+ const session = await getSessionFromCtx(ctx);
286
+ if (!session || existingUser.id !== session.user.id) throw APIError.from("BAD_REQUEST", USERNAME_ERROR_CODES.USERNAME_IS_ALREADY_TAKEN);
287
+ }
274
288
  }
275
- }
276
- const displayUsername = typeof ctx.body.displayUsername === "string" && options?.validationOrder?.displayUsername === "post-normalization" ? displayUsernameNormalizer(ctx.body.displayUsername) : ctx.body.displayUsername;
277
- if (displayUsername !== void 0 && typeof displayUsername === "string") {
278
- if (options?.displayUsernameValidator) {
279
- if (!await options.displayUsernameValidator(displayUsername)) throw APIError.from("BAD_REQUEST", USERNAME_ERROR_CODES.INVALID_DISPLAY_USERNAME);
289
+ const displayUsername = typeof ctx.body.displayUsername === "string" && options?.validationOrder?.displayUsername === "post-normalization" ? displayUsernameNormalizer(ctx.body.displayUsername) : ctx.body.displayUsername;
290
+ if (displayUsername !== void 0 && typeof displayUsername === "string") {
291
+ if (options?.displayUsernameValidator) {
292
+ if (!await options.displayUsernameValidator(displayUsername)) throw APIError.from("BAD_REQUEST", USERNAME_ERROR_CODES.INVALID_DISPLAY_USERNAME);
293
+ }
280
294
  }
281
- }
282
- })
283
- }, {
284
- matcher(context) {
285
- return context.path === "/sign-up/email";
295
+ })
286
296
  },
287
- handler: createAuthMiddleware(async (ctx) => {
288
- if (ctx.body.username && !ctx.body.displayUsername) ctx.body.displayUsername = ctx.body.username;
289
- if (ctx.body.displayUsername && !ctx.body.username) ctx.body.username = ctx.body.displayUsername;
290
- })
291
- }] },
297
+ {
298
+ matcher(context) {
299
+ return context.path === "/sign-up/email";
300
+ },
301
+ handler: createAuthMiddleware(async (ctx) => {
302
+ if (ctx.body.username && !ctx.body.displayUsername) ctx.body.displayUsername = ctx.body.username;
303
+ })
304
+ }
305
+ ] },
292
306
  options,
293
307
  $ERROR_CODES: USERNAME_ERROR_CODES
294
308
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "better-auth",
3
- "version": "1.6.20",
3
+ "version": "1.6.22",
4
4
  "description": "The most comprehensive authentication framework for TypeScript.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -483,19 +483,19 @@
483
483
  "@better-fetch/fetch": "1.3.1",
484
484
  "@noble/ciphers": "^2.1.1",
485
485
  "@noble/hashes": "^2.0.1",
486
- "better-call": "1.3.6",
486
+ "better-call": "1.3.7",
487
487
  "defu": "^6.1.4",
488
488
  "jose": "^6.1.3",
489
489
  "kysely": "^0.28.17 || ^0.29.0",
490
490
  "nanostores": "^1.1.1",
491
491
  "zod": "^4.3.6",
492
- "@better-auth/core": "1.6.20",
493
- "@better-auth/drizzle-adapter": "1.6.20",
494
- "@better-auth/kysely-adapter": "1.6.20",
495
- "@better-auth/memory-adapter": "1.6.20",
496
- "@better-auth/mongo-adapter": "1.6.20",
497
- "@better-auth/prisma-adapter": "1.6.20",
498
- "@better-auth/telemetry": "1.6.20"
492
+ "@better-auth/core": "1.6.22",
493
+ "@better-auth/drizzle-adapter": "1.6.22",
494
+ "@better-auth/kysely-adapter": "1.6.22",
495
+ "@better-auth/memory-adapter": "1.6.22",
496
+ "@better-auth/mongo-adapter": "1.6.22",
497
+ "@better-auth/prisma-adapter": "1.6.22",
498
+ "@better-auth/telemetry": "1.6.22"
499
499
  },
500
500
  "devDependencies": {
501
501
  "@lynx-js/react": "^0.116.3",
@@ -1,6 +0,0 @@
1
- import { BetterAuthOptions } from "@better-auth/core";
2
-
3
- //#region src/utils/get-request-ip.d.ts
4
- declare function getIp(req: Request | Headers, options: BetterAuthOptions): string | null;
5
- //#endregion
6
- export { getIp };
@@ -1,20 +0,0 @@
1
- import { isDevelopment, isTest } from "@better-auth/core/env";
2
- import { isValidIP, normalizeIP } from "@better-auth/core/utils/ip";
3
- //#region src/utils/get-request-ip.ts
4
- const LOCALHOST_IP = "127.0.0.1";
5
- function getIp(req, options) {
6
- if (options.advanced?.ipAddress?.disableIpTracking) return null;
7
- const headers = "headers" in req ? req.headers : req;
8
- const ipHeaders = options.advanced?.ipAddress?.ipAddressHeaders || ["x-forwarded-for"];
9
- for (const key of ipHeaders) {
10
- const value = "get" in headers ? headers.get(key) : headers[key];
11
- if (typeof value === "string") {
12
- const ip = value.split(",")[0].trim();
13
- if (isValidIP(ip)) return normalizeIP(ip, { ipv6Subnet: options.advanced?.ipAddress?.ipv6Subnet });
14
- }
15
- }
16
- if (isTest() || isDevelopment()) return LOCALHOST_IP;
17
- return null;
18
- }
19
- //#endregion
20
- export { getIp };