createOrganization in user -> after -> create hook returns null since user could not be found in the database #1946

Closed
opened 2026-03-13 09:14:17 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @Clex1o1 on GitHub (Sep 17, 2025).

Originally assigned to: @himself65 on GitHub.

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

Use an auth provider, sign in with google for example. And register the user create after hook:

databaseHooks: {
			user: {
				create: {
					after: async (user) => {
						try {
							const org = await auth.api.createOrganization({
								body: {
									name: getOrgName(user),
									slug: getId(),
									userId: user.id,
								},

							})
							if (org === null) {
								throw new Error("Failed to create organization");
							}
						} catch (err) {
							console.error(err);
							throw err;
						}
					},
				},
			},
...

New users do not have an organization ID and no new organization will be created. (Even if you not throw an error if no organization is returned by that method auth.api.createOrganization)

Current vs. Expected behavior

Current:
The method auth.api.createOrganization returns null. Inside, the 4b6a3c567d/packages/better-auth/src/plugins/organization/routes/crud-org.ts (L112), the ctx.context.internalAdapter.findUserById method returns also null.

Expected:
The current signed up user should be return and a new organization should be created inside the user creation after hook.

What version of Better Auth are you using?

1.3.11

System info

{
  "system": {
    "platform": "darwin",
    "arch": "arm64",
    "version": "Darwin Kernel Version 24.6.0: Mon Jul 14 11:30:40 PDT 2025; root:xnu-11417.140.69~1/RELEASE_ARM64_T6041",
    "release": "24.6.0",
    "cpuCount": 12,
    "cpuModel": "Apple M4 Pro",
    "totalMemory": "24.00 GB",
    "freeMemory": "0.46 GB"
  },
  "node": {
    "version": "v24.8.0",
    "env": "development"
  },
  "packageManager": {
    "name": "npm",
    "version": "11.6.0"
  },
  "frameworks": null,
  "databases": null,
  "betterAuth": {
    "version": "Unknown",
    "config": null
  }
}

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

Backend, Client

Auth config (if applicable)


Additional context

Hello!
Since the version 1.3.10 we cannot create an organization in the user -> create -> after hook.

databaseHooks: {
			user: {
				create: {
					after: async (user) => {
						try {
							const org = await auth.api.createOrganization({
								body: {
									name: getOrgName(user),
									slug: getId(),
									userId: user.id,
								},

							})
							if (org === null) {
								throw new Error("Failed to create organization");
							}
						} catch (err) {
							console.error(err);
							throw err;
						}
					},
				},
			},
...

The auth.api.createOrganization methods returns null. I was able to trace back the error to this point:
4b6a3c567d/packages/better-auth/src/plugins/organization/routes/crud-org.ts (L112)

The findUserById returns also null.

The version 1.3.9 works fine. 1.3.10 and 1.3.11 breaks.

Thanks for the help!

Originally created by @Clex1o1 on GitHub (Sep 17, 2025). Originally assigned to: @himself65 on GitHub. ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce Use an auth provider, sign in with google for example. And register the user create after hook: ``` databaseHooks: { user: { create: { after: async (user) => { try { const org = await auth.api.createOrganization({ body: { name: getOrgName(user), slug: getId(), userId: user.id, }, }) if (org === null) { throw new Error("Failed to create organization"); } } catch (err) { console.error(err); throw err; } }, }, }, ... ``` New users do not have an organization ID and no new organization will be created. (Even if you not throw an error if no organization is returned by that method `auth.api.createOrganization`) ### Current vs. Expected behavior **Current:** The method `auth.api.createOrganization` returns `null`. Inside, the https://github.com/better-auth/better-auth/blob/4b6a3c567d60b95640dae014b11d06eeca453be7/packages/better-auth/src/plugins/organization/routes/crud-org.ts#L112, the `ctx.context.internalAdapter.findUserById` method returns also null. **Expected:** The current signed up user should be return and a new organization should be created inside the user creation after hook. ### What version of Better Auth are you using? 1.3.11 ### System info ```bash { "system": { "platform": "darwin", "arch": "arm64", "version": "Darwin Kernel Version 24.6.0: Mon Jul 14 11:30:40 PDT 2025; root:xnu-11417.140.69~1/RELEASE_ARM64_T6041", "release": "24.6.0", "cpuCount": 12, "cpuModel": "Apple M4 Pro", "totalMemory": "24.00 GB", "freeMemory": "0.46 GB" }, "node": { "version": "v24.8.0", "env": "development" }, "packageManager": { "name": "npm", "version": "11.6.0" }, "frameworks": null, "databases": null, "betterAuth": { "version": "Unknown", "config": null } } ``` ### Which area(s) are affected? (Select all that apply) Backend, Client ### Auth config (if applicable) ```typescript ``` ### Additional context Hello! Since the version 1.3.10 we cannot create an organization in the **user** -> **create** -> **after** hook. ``` databaseHooks: { user: { create: { after: async (user) => { try { const org = await auth.api.createOrganization({ body: { name: getOrgName(user), slug: getId(), userId: user.id, }, }) if (org === null) { throw new Error("Failed to create organization"); } } catch (err) { console.error(err); throw err; } }, }, }, ... ``` The `auth.api.createOrganization` methods returns `null`. I was able to trace back the error to this point: https://github.com/better-auth/better-auth/blob/4b6a3c567d60b95640dae014b11d06eeca453be7/packages/better-auth/src/plugins/organization/routes/crud-org.ts#L112 The `findUserById` returns also `null`. The version 1.3.9 works fine. 1.3.10 and 1.3.11 breaks. Thanks for the help!
GiteaMirror added the bug label 2026-03-13 09:14:17 -05:00
Author
Owner

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

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

This regression is caused by a change in how Better Auth handles database transactions starting in v1.3.10. Now, user creation (and its hooks) may run inside a transaction that isn't committed until after all hooks finish. As a result, when your after hook calls auth.api.createOrganization, the new user isn't yet visible to findUserById, so organization creation fails and returns null (source).

There isn't currently documentation or a config option to change this timing. As a workaround, you could defer organization creation until after the transaction commits (for example, by queueing the action for later or triggering it from a different lifecycle event that runs post-commit). This is a side effect of the new transaction support and affects any logic in after hooks that depends on the user being immediately queryable from the database.

If you need to create an organization as soon as a user is created, you may need to move this logic outside the after hook or use a custom event that fires after the transaction is complete.

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 17, 2025): <!-- Greeting --> Hi @Clex1o1! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> This regression is caused by a change in how Better Auth handles database transactions starting in v1.3.10. Now, user creation (and its hooks) may run inside a transaction that isn't committed until after all hooks finish. As a result, when your after hook calls `auth.api.createOrganization`, the new user isn't yet visible to `findUserById`, so organization creation fails and returns `null` ([source](https://github.com/better-auth/better-auth/blob/5359f32778633dc7c46809de6b573016655fac5d/packages/better-auth/src/plugins/organization/routes/crud-org.ts)). There isn't currently documentation or a config option to change this timing. As a workaround, you could defer organization creation until after the transaction commits (for example, by queueing the action for later or triggering it from a different lifecycle event that runs post-commit). This is a side effect of the new transaction support and affects any logic in after hooks that depends on the user being immediately queryable from the database. If you need to create an organization as soon as a user is created, you may need to move this logic outside the after hook or use a custom event that fires after the transaction is complete. <!-- 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/e71f9837-ee0c-4f93-ab63-e838bfd157cd?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/e71f9837-ee0c-4f93-ab63-e838bfd157cd?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/e71f9837-ee0c-4f93-ab63-e838bfd157cd?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/e71f9837-ee0c-4f93-ab63-e838bfd157cd?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/e71f9837-ee0c-4f93-ab63-e838bfd157cd?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/e71f9837-ee0c-4f93-ab63-e838bfd157cd?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/e71f9837-ee0c-4f93-ab63-e838bfd157cd?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/4718)
Author
Owner

@himself65 commented on GitHub (Sep 17, 2025):

Checking the diff https://github.com/better-auth/better-auth/compare/v1.3.9...v1.3.10

@himself65 commented on GitHub (Sep 17, 2025): Checking the diff https://github.com/better-auth/better-auth/compare/v1.3.9...v1.3.10
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1946