[PR #3669] [MERGED] fix: respect username validator #4947

Closed
opened 2026-03-13 12:05:12 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/3669
Author: @azaek
Created: 7/28/2025
Status: Merged
Merged: 9/11/2025
Merged by: @himself65

Base: canaryHead: fix/username-validation-for-isavailable-check


📝 Commits (3)

  • 77c555e added validator call on isUsernameAvailable
  • b91d50f Update packages/better-auth/src/plugins/username/index.ts
  • 91fcb1e fix: improve

📊 Changes

2 files changed (+83 additions, -0 deletions)

View changed files

📝 packages/better-auth/src/plugins/username/index.ts (+24 -0)
📝 packages/better-auth/src/plugins/username/username.test.ts (+59 -0)

📄 Description

Found this issue with isUsernameAvailable function not validating the username before db query.

	isUsernameAvailable: createAuthEndpoint(
		"/is-username-available",
		{
			method: "POST",
			body: z.object({
				username: z.string().meta({
					description: "The username to check",
				}),
			}),
		},
		async (ctx) => {
			const username = ctx.body.username;
			if (!username) {
				throw new APIError("UNPROCESSABLE_ENTITY", {
					message: ERROR_CODES.INVALID_USERNAME,
				});
			}
			const user = await ctx.context.adapter.findOne<User>({
				model: "user",
				where: [
					{
						field: "username",
						value: username.toLowerCase(),
					},
				],
			});
			if (user) {
				return ctx.json({
					available: false,
				});
			}
			return ctx.json({
				available: true,
			});
		},
	),
},

Added the validator code to execute validation before db query

const validator =
options?.usernameValidator || defaultUsernameValidator;

if (!(await validator(username))) {
  throw new APIError("UNPROCESSABLE_ENTITY", {
	  message: ERROR_CODES.INVALID_USERNAME,
  });
}

Summary by cubic

Added username validation to the isUsernameAvailable endpoint to prevent invalid usernames from being checked against the database.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/3669 **Author:** [@azaek](https://github.com/azaek) **Created:** 7/28/2025 **Status:** ✅ Merged **Merged:** 9/11/2025 **Merged by:** [@himself65](https://github.com/himself65) **Base:** `canary` ← **Head:** `fix/username-validation-for-isavailable-check` --- ### 📝 Commits (3) - [`77c555e`](https://github.com/better-auth/better-auth/commit/77c555e0eeb6a99c1c440e2dd322bf72a5b3a7b4) added validator call on isUsernameAvailable - [`b91d50f`](https://github.com/better-auth/better-auth/commit/b91d50f7080e92d913ec693cec81c5f348f826bf) Update packages/better-auth/src/plugins/username/index.ts - [`91fcb1e`](https://github.com/better-auth/better-auth/commit/91fcb1e3027e93c05e709b184903f7ae4c21a0bb) fix: improve ### 📊 Changes **2 files changed** (+83 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/plugins/username/index.ts` (+24 -0) 📝 `packages/better-auth/src/plugins/username/username.test.ts` (+59 -0) </details> ### 📄 Description Found this issue with `isUsernameAvailable` function not validating the username before db query. ```ts isUsernameAvailable: createAuthEndpoint( "/is-username-available", { method: "POST", body: z.object({ username: z.string().meta({ description: "The username to check", }), }), }, async (ctx) => { const username = ctx.body.username; if (!username) { throw new APIError("UNPROCESSABLE_ENTITY", { message: ERROR_CODES.INVALID_USERNAME, }); } const user = await ctx.context.adapter.findOne<User>({ model: "user", where: [ { field: "username", value: username.toLowerCase(), }, ], }); if (user) { return ctx.json({ available: false, }); } return ctx.json({ available: true, }); }, ), }, ``` Added the validator code to execute validation before db query ```ts const validator = options?.usernameValidator || defaultUsernameValidator; if (!(await validator(username))) { throw new APIError("UNPROCESSABLE_ENTITY", { message: ERROR_CODES.INVALID_USERNAME, }); } ``` <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Added username validation to the isUsernameAvailable endpoint to prevent invalid usernames from being checked against the database. <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-03-13 12:05:12 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#4947