Username plugin doesn't ensure username uniqueness #389

Closed
opened 2026-03-13 07:44:12 -05:00 by GiteaMirror · 4 comments
Owner

Originally created by @modejota on GitHub (Dec 12, 2024).

Describe the bug
Adding the username plugin doesn't ensure the username uniqueness at application level, although it should.

To Reproduce
Steps to reproduce the behavior:

  1. Setup a register form in NextJs 15 that asks for both username and email.
  2. Submit the form using authClient.signup.email passing required fields, including an existing username value in the database.
  3. User is saved without ensuring username uniqueness at app level

Expected behavior
Based on the (schema), the username should be unique and checked at the application level, as well as email uniqueness is checked at app level and proper error message is returned if neccesary ({"message":"User with this email already exists","code":"USER_WITH_THIS_EMAIL_ALREADY_EXISTS"})

Screenshots
image

auth.ts and auth-client.ts. Database showing repeated username non-uniqueness

  • OS: Windows 11 Pro 23H2
  • Browser Google Chrome
  • Version 131.0.6778.139

Additional context
Scema definition suggests that the username should be unique, but it is not checked at the app level. It may be expected that uniqueness is ensured as it is done by email/password setup for the email. Please confirm if it is either a bug, the intended behaviour or perhaps some configuration or setup process is missing in the documentation

Originally created by @modejota on GitHub (Dec 12, 2024). **Describe the bug** Adding the username plugin doesn't ensure the username uniqueness at application level, although it should. **To Reproduce** Steps to reproduce the behavior: 1. Setup a register form in NextJs 15 that asks for both username and email. 2. Submit the form using `authClient.signup.email` passing required fields, including an existing username value in the database. 3. User is saved without ensuring username uniqueness at app level **Expected behavior** Based on the ([schema](https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/plugins/username/index.ts#L158)), the username should be unique and checked at the application level, as well as email uniqueness is checked at app level and proper error message is returned if neccesary (``{"message":"User with this email already exists","code":"USER_WITH_THIS_EMAIL_ALREADY_EXISTS"}``) **Screenshots** ![image](https://github.com/user-attachments/assets/04feacaa-8434-4d45-8b9d-e83b7266cc19) `auth.ts` and `auth-client.ts`. Database showing repeated username non-uniqueness - OS: Windows 11 Pro 23H2 - Browser Google Chrome - Version 131.0.6778.139 **Additional context** Scema definition suggests that the username should be unique, but it is not checked at the app level. It may be expected that uniqueness is ensured as it is done by email/password setup for the email. Please confirm if it is either a bug, the intended behaviour or perhaps some configuration or setup process is missing in the documentation
Author
Owner

@firatciftci commented on GitHub (Dec 16, 2024):

@Bekacru Apologies for commenting on a closed issue, but I am experiencing a similar problem here and I am a bit confused about your fix in response to the original issue. I am currently utilizing the username plugin with Better Auth, and I have migrated a previous auth database to Better Auth's schema. In this case, usernames are not expected to be all lowercase, but they are expected to be unique (in both lowercase and uppercase). For example, a user's username can be "Robert", but there cannot be another "robert" or "roBert" or any case variation of the original.

Any chance we can settle with a form where username uniqueness is checked in all lowercase, but usernames in the database are not autoformatted to be all lowercase? (The auto-lowercase functionality is a big problem for me in particular, as, for example, there is a "Robert" user, but signing up as "Robert" does the auto-lowercasing first, and then compares the lowercase version "robert" to "Robert", and the uniqueness check passes, which should not happen.)

@firatciftci commented on GitHub (Dec 16, 2024): @Bekacru Apologies for commenting on a closed issue, but I am experiencing a similar problem here and I am a bit confused about your fix in response to the original issue. I am currently utilizing the username plugin with Better Auth, and I have migrated a previous auth database to Better Auth's schema. In this case, usernames are not expected to be all lowercase, but they are expected to be unique (in both lowercase and uppercase). For example, a user's username can be "Robert", but there cannot be another "robert" or "roBert" or any case variation of the original. Any chance we can settle with a form where username uniqueness is checked in all lowercase, but usernames in the database are not autoformatted to be all lowercase? (The auto-lowercase functionality is a big problem for me in particular, as, for example, there is a "Robert" user, but signing up as "Robert" does the auto-lowercasing first, and then compares the lowercase version "robert" to "Robert", and the uniqueness check passes, which should not happen.)
Author
Owner

@benkingcode commented on GitHub (Feb 21, 2025):

Same here, the auto-lowercase behaviour is incompatible with our existing database

@benkingcode commented on GitHub (Feb 21, 2025): Same here, the auto-lowercase behaviour is incompatible with our existing database
Author
Owner

@marcusleonas commented on GitHub (Feb 23, 2025):

I'm also having this problem, username needs to be unique in my app

@marcusleonas commented on GitHub (Feb 23, 2025): I'm also having this problem, username needs to be unique in my app
Author
Owner

@Bekacru commented on GitHub (Feb 23, 2025):

Hey guys, you should try 1.2 rc pnpm i better-auth@1.2.0-beta.14. It includes displayUsername for non normalized use cases. You can find the docs at beta.better-auth.com

@Bekacru commented on GitHub (Feb 23, 2025): Hey guys, you should try 1.2 rc `pnpm i better-auth@1.2.0-beta.14`. It includes `displayUsername` for non normalized use cases. You can find the docs at beta.better-auth.com
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#389