More specific error codes for signInEmail method #377

Closed
opened 2026-03-13 07:43:54 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @octet-stream on GitHub (Dec 9, 2024).

Is your feature request related to a problem? Please describe.

Currently if Better Auth can't find a user the INVALID_EMAIL_OR_PASSWORD APIError is thrown. The problem is that I can't distinguish where the error occurred, even though the logs show what exactly happens. Because of this, I can't show a user proper error messages and highlight specific fields of a form like this:

Details

telegram-cloud-photo-size-2-5368446242687934559-y

Describe the solution you'd like

Add more specific error codes for signInEmail method when:

  1. A user was not found - this error is usually occurs if provided email is incorrect, so the INVALID_EMAIL error code would fit well, I can see it's already there;
  2. When Better Auth can't validate a user's password - the INVALID_PASSWORD will be perfect;
  3. A more specific error for credentials account may also be useful;

I understand that this will be a breaking change if somebody's already relied, but it will improve users experience with the apps that use Better Auth, and also help developers.

As alternative non-breaking solution I can suggest an extra property on the APIError's body, let's call it a field (or fields?) for example, and this property will work similarly to the code, but hold an information of what field caused the error. This will be similar to how you would specify an error in Conform: they have a fieldsError option in submission.reply method.

Describe alternatives you've considered

For now I can show an error for the whole form, and I'll kind of work, but I think it's not as helpful.

Additional context

I use Conform with React Router 7 to implement forms in my app and it allows me to send field errors from the server so I can then show them for each individual field.

Oh, and I'm using Better Auth v1.0.13 if that'll help.

And also, if I may ask you in the same issue: How can I import available error codes? I can't find it anywhere in the docs. But I may just miss something.

Originally created by @octet-stream on GitHub (Dec 9, 2024). **Is your feature request related to a problem? Please describe.** Currently if Better Auth can't find a user the `INVALID_EMAIL_OR_PASSWORD` APIError is thrown. The problem is that I can't distinguish where the error occurred, even though the logs show what exactly happens. Because of this, I can't show a user proper error messages and highlight specific fields of a form like this: <details> <summary>Details</summary> ![telegram-cloud-photo-size-2-5368446242687934559-y](https://github.com/user-attachments/assets/10a731da-149b-4de1-8819-bee3734e25d5) </details> **Describe the solution you'd like** Add more specific error codes for `signInEmail` method when: 1. A user was not found - this error is usually occurs if provided email is incorrect, so the `INVALID_EMAIL` error code would fit well, I can see it's [already there](https://github.com/better-auth/better-auth/blob/b86fd9321c014d09a8eab94de9b60034c740e186/packages/better-auth/src/api/routes/sign-in.ts#L338); 2. When Better Auth can't validate a user's password - the `INVALID_PASSWORD` will be perfect; 3. A more specific error for credentials account may also be useful; I understand that this will be a breaking change if somebody's already relied, but it will improve users experience with the apps that use Better Auth, and also help developers. As alternative non-breaking solution I can suggest an extra property on the APIError's `body`, let's call it a `field` (or fields?) for example, and this property will work similarly to the code, but hold an information of *what* field caused the error. This will be similar to how you would specify an error in Conform: they have a `fieldsError` option in `submission.reply` method. **Describe alternatives you've considered** For now I can show an error for the whole form, and I'll kind of work, but I think it's not as helpful. **Additional context** I use [Conform](https://conform.guide/) with React Router 7 to implement forms in my app and it allows me to send field errors from the server so I can then show them for each individual field. Oh, and I'm using Better Auth v1.0.13 if that'll help. And also, if I may ask you in the same issue: How can I import available error codes? I can't find it anywhere in the docs. But I may just miss something.
Author
Owner

@octet-stream commented on GitHub (Dec 9, 2024):

On other hand I could be wrong asking this, because technically this is not a validation error and so it may be a security risk to distinguish the two, because they reported after database call. I kind of missed this point somehow. Never mind.

@octet-stream commented on GitHub (Dec 9, 2024): On other hand I could be wrong asking this, because technically this is not a validation error and so it may be a security risk to distinguish the two, because they reported after database call. I kind of missed this point somehow. Never mind.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#377