fix(username): isUsernameAvailable should validate usernames (#3680)

This commit is contained in:
Maxwell
2025-08-01 16:50:22 +10:00
committed by GitHub
parent 5ce1473b19
commit e60f780f28
2 changed files with 29 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
---
"better-auth": patch
---
fix(username): `isUsernameAvailable` should run validation

View File

@@ -268,6 +268,30 @@ export const username = (options?: UsernameOptions) => {
message: ERROR_CODES.INVALID_USERNAME,
});
}
const minUsernameLength = options?.minUsernameLength || 3;
const maxUsernameLength = options?.maxUsernameLength || 30;
if (username.length < minUsernameLength) {
throw new APIError("UNPROCESSABLE_ENTITY", {
message: ERROR_CODES.USERNAME_TOO_SHORT,
});
}
if (username.length > maxUsernameLength) {
throw new APIError("UNPROCESSABLE_ENTITY", {
message: ERROR_CODES.USERNAME_TOO_LONG,
});
}
const validator =
options?.usernameValidator || defaultUsernameValidator;
const valid = await validator(username);
if (!valid) {
throw new APIError("UNPROCESSABLE_ENTITY", {
message: ERROR_CODES.INVALID_USERNAME,
});
}
const user = await ctx.context.adapter.findOne<User>({
model: "user",
where: [