feat(auth): multi-org enablement — system-scoped adapter reads + onOrganizationCreated hook#2323
Merged
Merged
Conversation
The better-auth -> ObjectQL adapter issued find/findOne/count without a caller context. On the cloud control plane, the org-scope read hook keys off the CALLER's user id to scope sys_member / sys_organization, so context-less adapter reads were filtered down to zero and organization.list() returned no orgs for a user who is a real member of one or more orgs (multi-org switching was unusable). Wrap the adapter's data engine in withSystemReadContext, marking every read context.isSystem:true so such hooks pass through. better-auth has already authenticated the session and scopes results by its own where-clauses (e.g. member.userId = session.user), so system reads are correct here. Writes are untouched (org-scope is a read-only hook). Applied to both createObjectQLAdapterFactory (production) and the legacy createObjectQLAdapter. Verified end-to-end against the cloud control plane: organization.list() now returns all of a user's orgs. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
📓 Docs Drift CheckThis PR changes 1 package(s): 10 hand-written doc(s) reference the affected code and may need an implementation-accuracy re-verification:
|
…ffects
better-auth's organization-plugin models (organization/member) do NOT fire
core databaseHooks, so a host stack had no server-side seam to run side
effects when a user creates a 2nd+ organization. Add an optional
onOrganizationCreated config callback, invoked from the org plugin's
afterCreateOrganization hook with { organizationId, userId, name, slug }.
Failure-isolated (org creation is never rolled back).
The cloud control plane uses this to uphold its born-with-environment
invariant: every organization, not just the signup/personal org, is born
with its production environment. Without it a user-created org landed
env-less and unusable.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…em-reads # Conflicts: # packages/plugins/plugin-auth/src/objectql-adapter.test.ts # packages/plugins/plugin-auth/src/objectql-adapter.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two framework changes that together make cloud multi-org usable. Paired with cloud PR objectstack-ai/cloud#507.
1. System-scoped adapter reads — fixes empty
organization.list()A user who belongs to ≥1 org got an empty
GET /api/v1/auth/organization/list(HTTP 200,[]), so the org switcher / Organizations page showed nothing — even with correctsys_memberrows.Root cause: better-auth resolves a user's orgs by querying the
membermodel through the ObjectQL adapter. Thosefind/findOne/countcalls carry no caller context. The cloud control-plane org-scope read hook scopessys_member/sys_organizationby the caller's user id, skipping only whencontext.isSystem— so context-less adapter reads were filtered to zero. (The active-org session hook was unaffected — it reads via the raw control driver, bypassing the engine hook.)Fix:
withSystemReadContext(engine)wraps a data engine so its reads (find/findOne/count) carrycontext.isSystem: true(merged; caller context wins on other keys). Applied in bothcreateObjectQLAdapterFactory(prod) and the legacycreateObjectQLAdapter. Writes untouched. Correct because better-auth already authenticated the session and scopes by its own where-clauses.2.
onOrganizationCreatedseam — born-with-environment for user-created orgsbetter-auth's org-plugin models (
organization/member) don't fire coredatabaseHooks, so a host stack had no server-side seam to run side effects on org creation. Add an optionalonOrganizationCreatedconfig callback, invoked from the org plugin'safterCreateOrganizationhook with{ organizationId, userId, name, slug }. Failure-isolated (org creation is never rolled back). The cloud control plane (#507) wires it to provision each new org's production environment — upholding its 1-prod-env-per-org "born-with-env" invariant for the 2nd+ org a user creates (the signup hook only covers the first org).Verification
withSystemReadContextcoverage (isSystem on reads, context merge, writes untouched).organization.list()returns all of a user's orgs (console org switcher + page render them; switching active org works); creating a 2nd org viacreateOrganizationprovisions itsos-<id>production env, immediately usable.🤖 Generated with Claude Code