[GH-ISSUE #73] Error handling for betterauth().api.signUpEmail() #25437

Closed
opened 2026-04-17 15:38:50 -05:00 by GiteaMirror · 4 comments
Owner

Originally created by @rdmchr on GitHub (Oct 3, 2024).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/73

I want to create a user account on the server side and as far as I can tell the right way to do this is, is the following:

// in a real code base there should be one central better auth instance, but this is just an example
betterAuth({
	database: drizzleAdapter(db, {
		provider: "pg", // or "mysql", "sqlite"
		schema: schema
	}),
	emailAndPassword: { enabled: true },
	logger: {
		verboseLogging: true,
	},
}).api.signUpEmail({body: {name: "XYZ", email: "example@example.com", password: "password"}});

The problem with this is that there is no error handling at all. The types indicate that you either get the user/session or null. Even wrapping this statement with a try/catch did not help as the catch statement never gets triggered.

I listed a few ways of solving this issue and would like to hear from a maintainer how they think this should be solved.

  1. If this is not the intended way to use this library (i.e. all users should be required to register on their own) I could just create a custom fetch call to the API endpoint.
  2. Adding an option to the createAuthClient().signUp.email() to NOT create a new session after creating the account. This would allow me to just call that function on my admin page without having to create my own API endpoint.
  3. Make the signUpEmail function throw or return errors. The errors are already logged when using verboseLogging, so they should already exist somewhere. Maybe exposing them is not too much work.

Quick note: I changed the title since I only tested that one function, but this might impact other functions on the betterauth().api as well.

Originally created by @rdmchr on GitHub (Oct 3, 2024). Original GitHub issue: https://github.com/better-auth/better-auth/issues/73 I want to create a user account on the server side and as far as I can tell the right way to do this is, is the following: ```TS // in a real code base there should be one central better auth instance, but this is just an example betterAuth({ database: drizzleAdapter(db, { provider: "pg", // or "mysql", "sqlite" schema: schema }), emailAndPassword: { enabled: true }, logger: { verboseLogging: true, }, }).api.signUpEmail({body: {name: "XYZ", email: "example@example.com", password: "password"}}); ``` The problem with this is that there is no error handling at all. The types indicate that you either get the user/session or `null`. Even wrapping this statement with a try/catch did not help as the catch statement never gets triggered. I listed a few ways of solving this issue and would like to hear from a maintainer how they think this should be solved. 1. If this is not the intended way to use this library (i.e. all users should be required to register on their own) I could just create a custom `fetch` call to the API endpoint. 2. Adding an option to the `createAuthClient().signUp.email()` to NOT create a new session after creating the account. This would allow me to just call that function on my admin page without having to create my own API endpoint. 3. Make the `signUpEmail` function throw or return errors. The errors are already logged when using `verboseLogging`, so they should already exist somewhere. Maybe exposing them is not too much work. Quick note: I changed the title since I only tested that one function, but this might impact other functions on the `betterauth().api` as well.
GiteaMirror added the locked label 2026-04-17 15:38:50 -05:00
Author
Owner

@Bekacru commented on GitHub (Oct 3, 2024):

We definitely need to improve the error handling on the server side. While discussing it, we were torn between returning errors as values or throwing them. Handling errors as values adds unnecessary complexity, so I think we should move everything to throw an "APIError" on the server instead. That being said, the "signUp.email" still creates a session on the server side. We probably should have createUser action that's not exposed to the client.

<!-- gh-comment-id:2390552908 --> @Bekacru commented on GitHub (Oct 3, 2024): We definitely need to improve the error handling on the server side. While discussing it, we were torn between returning errors as values or throwing them. Handling errors as values adds unnecessary complexity, so I think we should move everything to throw an "APIError" on the server instead. That being said, the "signUp.email" still creates a session on the server side. We probably should have `createUser` action that's not exposed to the client.
Author
Owner

@rdmchr commented on GitHub (Oct 3, 2024):

Yeah, functions to manage other users as an administrator would be great!

<!-- gh-comment-id:2391315097 --> @rdmchr commented on GitHub (Oct 3, 2024): Yeah, functions to manage other users as an administrator would be great!
Author
Owner

@Bekacru commented on GitHub (Oct 3, 2024):

Yeah, functions to manage other users as an administrator would be great!

there is an admin panel in the works (similar to clerk's user management ui). It'll come with an admin plugin that adds these methods.

<!-- gh-comment-id:2392254820 --> @Bekacru commented on GitHub (Oct 3, 2024): > Yeah, functions to manage other users as an administrator would be great! there is an admin panel in the works (similar to clerk's user management ui). It'll come with an admin plugin that adds these methods.
Author
Owner

@rdmchr commented on GitHub (Oct 3, 2024):

That sounds lovely. I don't need a full UI, but just having access to those methods will be wonderful. Great project so far ♥

<!-- gh-comment-id:2392355090 --> @rdmchr commented on GitHub (Oct 3, 2024): That sounds lovely. I don't need a full UI, but just having access to those methods will be wonderful. Great project so far ♥
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#25437