Bug: wrong auth error code on repeat register #1831

Closed
opened 2026-03-13 09:06:27 -05:00 by GiteaMirror · 6 comments
Owner

Originally created by @CarrettaRiccardo on GitHub (Sep 3, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

When trying to sign up a user which was already signed up, the error code USER_ALREADY_EXISTS_USE_ANOTHER_EMAIL is thrown, which is not listed!

In other words:

  • sign up
  • sign up again
  • error code thrown: USER_ALREADY_EXISTS_USE_ANOTHER_EMAIL

Example project (not mine): https://github.com/patelharsh9797/t3_stack_better_auth

Current vs. Expected behavior

Error code USER_ALREADY_EXISTS_USE_ANOTHER_EMAIL is not listed in possible error codes
(the most similar name is USER_ALREADY_EXISTS)

See BASE_ERROR_CODES list: https://github.com/better-auth/better-auth/blob/canary/packages/better-auth/src/error/codes.ts

What version of Better Auth are you using?

1.3.7

System info

System:
    OS: macOS 15.6.1
    CPU: (12) arm64 Apple M4 Pro
    Memory: 335.06 MB / 24.00 GB
    Shell: 5.9 - /bin/zsh
  Browsers:
    Chrome: 139.0.7258.157
    Safari: 18.6

Which area(s) are affected? (Select all that apply)

Client

Auth config (if applicable)

import { betterAuth } from "better-auth"
export const auth = betterAuth({
  emailAndPassword: {  
    enabled: true
  },
});

Additional context

No response

Originally created by @CarrettaRiccardo on GitHub (Sep 3, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce When trying to sign up a user which was already signed up, the error code `USER_ALREADY_EXISTS_USE_ANOTHER_EMAIL` is thrown, which is not listed! In other words: - sign up - sign up again - error code thrown: USER_ALREADY_EXISTS_USE_ANOTHER_EMAIL Example project (not mine): https://github.com/patelharsh9797/t3_stack_better_auth ### Current vs. Expected behavior Error code USER_ALREADY_EXISTS_USE_ANOTHER_EMAIL is not listed in possible error codes (the most similar name is USER_ALREADY_EXISTS) See BASE_ERROR_CODES list: https://github.com/better-auth/better-auth/blob/canary/packages/better-auth/src/error/codes.ts ### What version of Better Auth are you using? 1.3.7 ### System info ```bash System: OS: macOS 15.6.1 CPU: (12) arm64 Apple M4 Pro Memory: 335.06 MB / 24.00 GB Shell: 5.9 - /bin/zsh Browsers: Chrome: 139.0.7258.157 Safari: 18.6 ``` ### Which area(s) are affected? (Select all that apply) Client ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ emailAndPassword: { enabled: true }, }); ``` ### Additional context _No response_
GiteaMirror added the bug label 2026-03-13 09:06:27 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Sep 3, 2025):

Hi @CarrettaRiccardo! I'm Dosu and I’m helping the better-auth team.

The error code USER_ALREADY_EXISTS_USE_ANOTHER_EMAIL does not exist in the Better Auth codebase or documentation. The actual error code thrown for duplicate registration attempts is USER_ALREADY_EXISTS, but its message is "User already exists. Use another email." This message may have led to confusion, making it seem like a different error code was being used. The code and recent updates confirm that USER_ALREADY_EXISTS is the correct and intended error code for this scenario, and only its message was updated for clarity—not the code itself. You can safely handle USER_ALREADY_EXISTS for repeat registration cases in your client code.
See supporting code and PR | Recent PR clarifying the message

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Chat with Dosu Join Discord Share on X

@dosubot[bot] commented on GitHub (Sep 3, 2025): <!-- Greeting --> Hi @CarrettaRiccardo! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> The error code USER_ALREADY_EXISTS_USE_ANOTHER_EMAIL does not exist in the Better Auth codebase or documentation. The actual error code thrown for duplicate registration attempts is USER_ALREADY_EXISTS, but its message is "User already exists. Use another email." This message may have led to confusion, making it seem like a different error code was being used. The code and recent updates confirm that USER_ALREADY_EXISTS is the correct and intended error code for this scenario, and only its message was updated for clarity—not the code itself. You can safely handle USER_ALREADY_EXISTS for repeat registration cases in your client code. [See supporting code and PR](https://github.com/better-auth/better-auth/blob/23daf79a427ea987a4d94d0431e0fb306d26d464/packages/better-auth/src/error/codes.ts) | [Recent PR clarifying the message](https://github.com/better-auth/better-auth/pull/3957) <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/33176281-0b20-4d2d-bd48-fae0c1824a8a?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/33176281-0b20-4d2d-bd48-fae0c1824a8a?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/33176281-0b20-4d2d-bd48-fae0c1824a8a?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/33176281-0b20-4d2d-bd48-fae0c1824a8a?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/33176281-0b20-4d2d-bd48-fae0c1824a8a?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/33176281-0b20-4d2d-bd48-fae0c1824a8a?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/33176281-0b20-4d2d-bd48-fae0c1824a8a?feedback_type=other)</sup>&nbsp;&nbsp;[![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/issues/4386)
Author
Owner

@Zollerboy1 commented on GitHub (Sep 9, 2025):

Just hit this problem as well. The culprit seems to be that the API routes don't actually construct errors using the error codes, but only using their messages. The messages are given to the better-call library, which generates new codes from the messages (by uppercasing them, replacing spaces with underscores, and removing non-alpha-numeric characters–see this code). For most of better-auth's error codes, this transformation generates an equal code, however, this is not the case for USER_ALREADY_EXISTS, SESSION_EXPIRED, FAILED_TO_UNLINK_LAST_ACCOUNT, and USER_ALREADY_HAS_PASSWORD, because their messages contain different text.

@Zollerboy1 commented on GitHub (Sep 9, 2025): Just hit this problem as well. The culprit seems to be that the API routes don't actually construct errors using the error codes, but only using their messages. The messages are given to the [better-call](https://github.com/Bekacru/better-call) library, which generates new codes from the messages (by uppercasing them, replacing spaces with underscores, and removing non-alpha-numeric characters–see [this code](https://github.com/Bekacru/better-call/blob/215c18634c5488814d9b5b9c81e5394d084f2154/src/error.ts#L217C1-L221C14)). For most of better-auth's error codes, this transformation generates an equal code, however, this is not the case for `USER_ALREADY_EXISTS`, `SESSION_EXPIRED`, `FAILED_TO_UNLINK_LAST_ACCOUNT`, and `USER_ALREADY_HAS_PASSWORD`, because their messages contain different text.
Author
Owner

@bastiengrignon commented on GitHub (Sep 14, 2025):

Ran into the same issue here !
Is there a way to the api to return the right USER_ALREADY_EXISTS code instead of USER_ALREADY_EXISTS_USE_ANOTHER_EMAIL or I need to handle it myself ?
Another issue is that I'm using better-auth-localization and it translate error codes from better-auth ERROR_CODES constants and as this USER_ALREADY_EXISTS_USE_ANOTHER_EMAIL is not in it the key is not translated !

I don't know if this key should be added to the constants or that the API should not generate the code from the message 🤔

@bastiengrignon commented on GitHub (Sep 14, 2025): Ran into the same issue here ! Is there a way to the api to return the right `USER_ALREADY_EXISTS` code instead of `USER_ALREADY_EXISTS_USE_ANOTHER_EMAIL` or I need to handle it myself ? Another issue is that I'm using `better-auth-localization` and it translate error codes from better-auth `ERROR_CODES` constants and as this `USER_ALREADY_EXISTS_USE_ANOTHER_EMAIL` is not in it the key is not translated ! I don't know if this key should be added to the constants or that the API should not generate the code from the message 🤔
Author
Owner

@dvanmali commented on GitHub (Sep 24, 2025):

Wanted to check if this issue is still persistent? USER_ALREADY_EXISTS appears to be the error code in the repo. To obtain error codes, here's a doc.

@dvanmali commented on GitHub (Sep 24, 2025): Wanted to check if this issue is still persistent? `USER_ALREADY_EXISTS` appears to be the error code in the repo. To obtain error codes, here's a [doc](https://www.better-auth.com/docs/concepts/client#handling-errors).
Author
Owner

@Zollerboy1 commented on GitHub (Sep 25, 2025):

@dvanmali yes, this still happens in the latest version of better-auth (1.3.17 as of writing this comment). Please refer to my comment above where I outlined my investigation about why the client gets this error code even though that code doesn't exist in the error code definitions.

@Zollerboy1 commented on GitHub (Sep 25, 2025): @dvanmali yes, this still happens in the latest version of better-auth (1.3.17 as of writing this comment). Please refer to my comment above where I outlined my investigation about why the client gets this error code even though that code doesn't exist in the error code definitions.
Author
Owner

@dvanmali commented on GitHub (Sep 25, 2025):

@Zollerboy1 thanks for the insight. I was able to reproduce and will patch this

@dvanmali commented on GitHub (Sep 25, 2025): @Zollerboy1 thanks for the insight. I was able to reproduce and will patch this
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1831