mirror of
https://github.com/better-auth/better-auth.git
synced 2026-05-25 00:22:43 -05:00
fix(username): isUsernameAvailable should validate usernames (#3680)
This commit is contained in:
5
.changeset/wild-pianos-join.md
Normal file
5
.changeset/wild-pianos-join.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"better-auth": patch
|
||||
---
|
||||
|
||||
fix(username): `isUsernameAvailable` should run validation
|
||||
@@ -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: [
|
||||
|
||||
Reference in New Issue
Block a user