[GH-ISSUE #6524] Unexpected createOrganization behavior #19171

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

Originally created by @ZuluNovember on GitHub (Dec 4, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/6524

Organization creation has some surprising behaviors. This might be intentional or not but after looking at the documentation I had different mental modal which I had to dive in to code to understand.

This is the server side code example in docs:

const metadata = { someKey: "someValue" };

const data = await auth.api.createOrganization({
    body: {
        name: "My Organization", // required
        slug: "my-org", // required
        logo: "https://example.com/logo.png",
        metadata,
        userId: "some_user_id", // server-only
        keepCurrentActiveOrganization: false,
    },
    // This endpoint requires session cookies.
    headers: await headers(),
});
  • In the example it states headers should be included in the request. Including headers with an authenticated user makes userId in body ignored.
    source
let user = session?.user || null;
if (!user) {
	if (!ctx.body.userId) {
		throw new APIError("UNAUTHORIZED");
	}
	user = await ctx.context.internalAdapter.findUserById(ctx.body.userId);
}
  • Other unexpected issue for me was that every other check such as allowUserToCreateOrganization runs based on the user set in above example. So I can't do checks like user.role === 'admin' and allow admins to create organizations for other users. I'd expect creating user to be able to different than organization owner user.

I don't know how much of this behavior is intended but I think at least header part should be better documented.

Originally created by @ZuluNovember on GitHub (Dec 4, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/6524 Organization creation has some surprising behaviors. This might be intentional or not but after looking at the [documentation](https://www.better-auth.com/docs/plugins/organization#create-an-organization) I had different mental modal which I had to dive in to code to understand. This is the server side code example in docs: ```js const metadata = { someKey: "someValue" }; const data = await auth.api.createOrganization({ body: { name: "My Organization", // required slug: "my-org", // required logo: "https://example.com/logo.png", metadata, userId: "some_user_id", // server-only keepCurrentActiveOrganization: false, }, // This endpoint requires session cookies. headers: await headers(), }); ``` - In the example it states headers should be included in the request. Including headers with an authenticated user makes `userId` in body ignored. [source](https://github.com/better-auth/better-auth/blob/95dddf0b543928559f889ae317b25dde7159626d/packages/better-auth/src/plugins/organization/routes/crud-org.ts#L105-L111) ```js let user = session?.user || null; if (!user) { if (!ctx.body.userId) { throw new APIError("UNAUTHORIZED"); } user = await ctx.context.internalAdapter.findUserById(ctx.body.userId); } ``` - Other unexpected issue for me was that every other check such as `allowUserToCreateOrganization` runs based on the user set in above example. So I can't do checks like `user.role === 'admin'` and allow admins to create organizations for other users. I'd expect creating user to be able to different than organization owner user. I don't know how much of this behavior is intended but I think at least header part should be better documented.
GiteaMirror added the locked label 2026-04-15 17:58:38 -05:00
Author
Owner

@better-auth-agent[bot] commented on GitHub (Dec 4, 2025):

Hi there, thanks for raising this issue! When creating an organization via the API, if you pass session headers (i.e., an authenticated request), the API will use the session’s user rather than a userId from the request body. This means that any provided userId gets ignored, which in turn forces policy checks such as allowUserToCreateOrganization on that session user. In other words, it’s designed so that the organization’s creator is always the authenticated user, not an arbitrary user from request data. This design currently prevents scenarios where an admin might create an organization on behalf of another user.

Would you consider a custom extension or discussing an enhancement with the team to support admin-led organization creation? Tag @better-auth-agent if you would like an answer from the Agent.

If you need more help, tag @better-auth-agent in a comment so I can respond.

DiagramDiscordGitHub

Diagram Join Star

<!-- gh-comment-id:3611538156 --> @better-auth-agent[bot] commented on GitHub (Dec 4, 2025): Hi there, thanks for raising this issue! When creating an organization via the API, if you pass session headers (i.e., an authenticated request), the API will use the session’s user rather than a userId from the request body. This means that any provided userId gets ignored, which in turn forces policy checks such as allowUserToCreateOrganization on that session user. In other words, it’s designed so that the organization’s creator is always the authenticated user, not an arbitrary user from request data. This design currently prevents scenarios where an admin might create an organization on behalf of another user. Would you consider a custom extension or discussing an enhancement with the team to support admin-led organization creation? Tag @better-auth-agent if you would like an answer from the Agent. _If you need more help, tag @better-auth-agent in a comment so I can respond._ <!-- bot:webhook reply v1 --> [Diagram](https://repodiagrams.s3.eu-north-1.amazonaws.com/skyvern_ultra_detailed_interactive.html) • [Discord](https://discord.gg/fG2XXEuQX3) • [GitHub](https://github.com/Skyvern-AI/Skyvern) [![Diagram](https://img.shields.io/badge/Diagram-2b3137?style=flat-square)](https://repodiagrams.s3.eu-north-1.amazonaws.com/skyvern_ultra_detailed_interactive.html) [![Join](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&style=flat-square)](https://discord.gg/fG2XXEuQX3) [![Star](https://img.shields.io/badge/star-181717?logo=github&logoColor=white&style=flat-square)](https://github.com/Skyvern-AI/Skyvern)
Author
Owner

@GautamBytes commented on GitHub (Dec 4, 2025):

Looking into it!!

<!-- gh-comment-id:3612094556 --> @GautamBytes commented on GitHub (Dec 4, 2025): Looking into it!!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#19171