admin operation not executing the "before" hook. #1499

Closed
opened 2026-03-13 08:43:31 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @pranavmappoli on GitHub (Jul 15, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

i have a database hook like below to auto generate a userName

	databaseHooks: {
		user: {
			create: {
				before: async (user, ctx) => {
					// Generate unique username from email
					const userName = generateUniqueUsername(user.email);

					return {
						data: {
							...user,
							userName,
						},
					};
				},
			},
		},
	},

when i create a user through admin,
`await authClient.admin.createUser()` the before hook is not executing,
also iam not able to pass the username through createUser()

### Current vs. Expected behavior

it should execute before hook as well

### What version of Better Auth are you using?

lastest

### Provide environment information

```bash
chrome

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

Backend

Auth config (if applicable)

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

Additional context

No response

Originally created by @pranavmappoli on GitHub (Jul 15, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce i have a database hook like below to auto generate a userName ``` databaseHooks: { user: { create: { before: async (user, ctx) => { // Generate unique username from email const userName = generateUniqueUsername(user.email); return { data: { ...user, userName, }, }; }, }, }, }, when i create a user through admin, `await authClient.admin.createUser()` the before hook is not executing, also iam not able to pass the username through createUser() ### Current vs. Expected behavior it should execute before hook as well ### What version of Better Auth are you using? lastest ### Provide environment information ```bash chrome ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ emailAndPassword: { enabled: true }, }); ``` ### Additional context _No response_
Author
Owner

@dosubot[bot] commented on GitHub (Jul 15, 2025):

The admin API (authClient.admin.createUser()) in Better Auth does not trigger the standard before database hooks by default—this is due to how admin operations are implemented, likely for performance or privilege reasons. As a result, your before hook for auto-generating a userName won't run when using the admin API, and there isn't a built-in way to enable this behavior or to pass userName directly unless the admin API explicitly supports it in its parameters. This is a known limitation in the current architecture source.

If you need to ensure consistent data processing (like username generation) for admin-created users, you have a couple of options:

  • Use the adapter's customTransformInput configuration to inject your logic for generating a username before the data is persisted. This is not the same as a lifecycle hook, but it can achieve a similar effect for all user creation, including admin operations. You'd need to add your username generation logic to the adapter config.
  • As a workaround, you could wrap or monkey-patch the admin createUser method to call your username generation logic before invoking the actual creation, but this is more of a hack and less maintainable.

There is currently no documented or supported way to have admin operations run the same hooks as standard user creation. If you want to keep all logic in one place, the adapter-level transformation is your best bet.

To reply, just mention @dosu.


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

@dosubot[bot] commented on GitHub (Jul 15, 2025): <!-- Answer --> The admin API (`authClient.admin.createUser()`) in Better Auth does not trigger the standard `before` database hooks by default—this is due to how admin operations are implemented, likely for performance or privilege reasons. As a result, your `before` hook for auto-generating a `userName` won't run when using the admin API, and there isn't a built-in way to enable this behavior or to pass `userName` directly unless the admin API explicitly supports it in its parameters. This is a known limitation in the current architecture [source](https://github.com/better-auth/better-auth/blob/be511008f12eb31111f865e994751957702431d4/packages/better-auth/src/adapters/create-adapter/index.ts). If you need to ensure consistent data processing (like username generation) for admin-created users, you have a couple of options: - Use the adapter's `customTransformInput` configuration to inject your logic for generating a username before the data is persisted. This is not the same as a lifecycle hook, but it can achieve a similar effect for all user creation, including admin operations. You'd need to add your username generation logic to the adapter config. - As a workaround, you could wrap or monkey-patch the admin `createUser` method to call your username generation logic before invoking the actual creation, but this is more of a hack and less maintainable. There is currently no documented or supported way to have admin operations run the same hooks as standard user creation. If you want to keep all logic in one place, the adapter-level transformation is your best bet. <!-- 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/8fe1314c-bc49-42d8-b831-8adfefc02bea?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/8fe1314c-bc49-42d8-b831-8adfefc02bea?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/8fe1314c-bc49-42d8-b831-8adfefc02bea?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/8fe1314c-bc49-42d8-b831-8adfefc02bea?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/8fe1314c-bc49-42d8-b831-8adfefc02bea?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/8fe1314c-bc49-42d8-b831-8adfefc02bea?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/8fe1314c-bc49-42d8-b831-8adfefc02bea?feedback_type=other)</sup>&nbsp;&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/3389)
Author
Owner

@pranavmappoli commented on GitHub (Jul 17, 2025):

@Kinfe123 @Bekacru Why the auth0 (Email & Password) signup also not triggering the "before" hook ?
it seems only social media create supports.

@pranavmappoli commented on GitHub (Jul 17, 2025): @Kinfe123 @Bekacru Why the auth0 (Email & Password) signup also not triggering the "before" hook ? it seems only social media create supports.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1499