[PR #8731] [MERGED] fix(username): enforce username uniqueness in updateUser #25077

Closed
opened 2026-04-15 22:42:59 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/8731
Author: @aarmful
Created: 3/22/2026
Status: Merged
Merged: 3/24/2026
Merged by: @ping-maxwell

Base: canaryHead: fix/update-user-username-uniqueness


📝 Commits (3)

  • aa838f5 fix(username): enforce username uniqueness in updateUser
  • a253fc3 fix(username): avoid double-normalization in uniqueness lookup
  • 583fa27 Merge branch 'canary' into fix/update-user-username-uniqueness

📊 Changes

2 files changed (+48 additions, -14 deletions)

View changed files

📝 packages/better-auth/src/plugins/username/index.ts (+15 -9)
📝 packages/better-auth/src/plugins/username/username.test.ts (+33 -5)

📄 Description

closes #8689

updateUser didn't actually enforce username uniqueness, so users could set usernames already taken by others.

the before hook had a uniqueness check but it was dead code because ctx.context.session is always null in before hooks.

fixed by resolving the session via getSessionFromCtx() instead. also normalized the username before the db lookup to match what isUsernameAvailable does, which prevents case-different duplicates from slipping through.

also fixed an existing test that was passing for the wrong reason. it used "duplicate-username" which has a hyphen that fails the validator, so it was testing format validation, not uniqueness.


Summary by cubic

Enforces username uniqueness across sign-up and update with case-insensitive checks and correct session handling. Conflicts now return 400 and only allow the current user to keep their own username.

  • Bug Fixes
    • Use getSessionFromCtx() in /update-user to verify the requester before allowing a duplicate.
    • Normalize the username once before the DB lookup to prevent case-only duplicates and avoid double-normalization.
    • Block duplicates on /sign-up/email and /update-user with 400; update tests to use valid duplicate usernames and add a case-variant check.

Written for commit 583fa27053. Summary will update on new commits.


🔄 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/8731 **Author:** [@aarmful](https://github.com/aarmful) **Created:** 3/22/2026 **Status:** ✅ Merged **Merged:** 3/24/2026 **Merged by:** [@ping-maxwell](https://github.com/ping-maxwell) **Base:** `canary` ← **Head:** `fix/update-user-username-uniqueness` --- ### 📝 Commits (3) - [`aa838f5`](https://github.com/better-auth/better-auth/commit/aa838f56ff5e176dd0a9d7238727b05133733d5f) fix(username): enforce username uniqueness in updateUser - [`a253fc3`](https://github.com/better-auth/better-auth/commit/a253fc3c3f48e6ab256433fc733931f44c2a6e50) fix(username): avoid double-normalization in uniqueness lookup - [`583fa27`](https://github.com/better-auth/better-auth/commit/583fa270530ef6fd6bcda32e919d4fc4de2a64c9) Merge branch 'canary' into fix/update-user-username-uniqueness ### 📊 Changes **2 files changed** (+48 additions, -14 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/plugins/username/index.ts` (+15 -9) 📝 `packages/better-auth/src/plugins/username/username.test.ts` (+33 -5) </details> ### 📄 Description closes #8689 `updateUser` didn't actually enforce username uniqueness, so users could set usernames already taken by others. the before hook had a uniqueness check but it was dead code because `ctx.context.session` is always `null` in before hooks. fixed by resolving the session via `getSessionFromCtx()` instead. also normalized the username before the db lookup to match what `isUsernameAvailable` does, which prevents case-different duplicates from slipping through. also fixed an existing test that was passing for the wrong reason. it used `"duplicate-username"` which has a hyphen that fails the validator, so it was testing format validation, not uniqueness. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Enforces username uniqueness across sign-up and update with case-insensitive checks and correct session handling. Conflicts now return 400 and only allow the current user to keep their own username. - **Bug Fixes** - Use `getSessionFromCtx()` in `/update-user` to verify the requester before allowing a duplicate. - Normalize the username once before the DB lookup to prevent case-only duplicates and avoid double-normalization. - Block duplicates on `/sign-up/email` and `/update-user` with 400; update tests to use valid duplicate usernames and add a case-variant check. <sup>Written for commit 583fa270530ef6fd6bcda32e919d4fc4de2a64c9. Summary will update on new commits.</sup> <!-- 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-04-15 22:42:59 -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#25077