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.
- package/dist/api/index.d.mts +3 -3
- package/dist/api/index.mjs +5 -5
- package/dist/api/rate-limiter/index.mjs +2 -3
- package/dist/api/routes/index.d.mts +1 -1
- package/dist/api/routes/session.d.mts +30 -3
- package/dist/api/routes/session.mjs +15 -4
- package/dist/context/create-context.mjs +6 -0
- package/dist/db/get-migration.mjs +1 -0
- package/dist/db/get-schema.d.mts +1 -0
- package/dist/db/get-schema.mjs +3 -1
- package/dist/db/index.d.mts +3 -2
- package/dist/db/index.mjs +5 -2
- package/dist/db/internal-adapter.mjs +1 -1
- package/dist/db/revoke-unproven-account-access.d.mts +19 -0
- package/dist/db/revoke-unproven-account-access.mjs +23 -0
- package/dist/db/schema.d.mts +2 -1
- package/dist/db/schema.mjs +13 -1
- package/dist/oauth2/link-account.mjs +20 -7
- package/dist/package.mjs +1 -1
- package/dist/plugins/admin/routes.mjs +4 -4
- package/dist/plugins/captcha/index.mjs +1 -1
- package/dist/plugins/device-authorization/index.d.mts +9 -9
- package/dist/plugins/device-authorization/index.mjs +1 -1
- package/dist/plugins/email-otp/routes.mjs +5 -1
- package/dist/plugins/magic-link/index.d.mts +3 -3
- package/dist/plugins/magic-link/index.mjs +6 -2
- package/dist/plugins/oauth-proxy/index.mjs +11 -6
- package/dist/plugins/one-tap/index.mjs +10 -9
- package/dist/plugins/siwe/index.mjs +5 -1
- package/dist/plugins/two-factor/backup-codes/index.mjs +21 -7
- package/dist/plugins/two-factor/client.d.mts +1 -0
- package/dist/plugins/two-factor/error-code.d.mts +1 -0
- package/dist/plugins/two-factor/error-code.mjs +1 -0
- package/dist/plugins/two-factor/index.d.mts +14 -0
- package/dist/plugins/two-factor/index.mjs +7 -1
- package/dist/plugins/two-factor/otp/index.mjs +17 -1
- package/dist/plugins/two-factor/schema.d.mts +13 -0
- package/dist/plugins/two-factor/schema.mjs +13 -0
- package/dist/plugins/two-factor/totp/index.mjs +23 -9
- package/dist/plugins/two-factor/types.d.mts +25 -0
- package/dist/plugins/two-factor/verify-two-factor.mjs +110 -3
- package/dist/plugins/username/index.mjs +56 -42
- package/package.json +9 -9
- package/dist/utils/get-request-ip.d.mts +0 -6
- 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
|
|
37
|
-
if (
|
|
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
|
-
|
|
252
|
-
|
|
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
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
if (username
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
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
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
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
|
-
|
|
288
|
-
|
|
289
|
-
|
|
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.
|
|
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.
|
|
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.
|
|
493
|
-
"@better-auth/drizzle-adapter": "1.6.
|
|
494
|
-
"@better-auth/kysely-adapter": "1.6.
|
|
495
|
-
"@better-auth/memory-adapter": "1.6.
|
|
496
|
-
"@better-auth/mongo-adapter": "1.6.
|
|
497
|
-
"@better-auth/prisma-adapter": "1.6.
|
|
498
|
-
"@better-auth/telemetry": "1.6.
|
|
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,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 };
|