Commit 0885fde
feat(auth): multi-org enablement — system-scoped adapter reads + onOrganizationCreated hook (#2323)
* fix(auth): run better-auth objectql adapter reads as system reads
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>
* feat(auth): add onOrganizationCreated seam for host org-create side effects
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>
---------
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>1 parent 21d4f89 commit 0885fde
3 files changed
Lines changed: 116 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
165 | 165 | | |
166 | 166 | | |
167 | 167 | | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
168 | 183 | | |
169 | 184 | | |
170 | 185 | | |
| |||
744 | 759 | | |
745 | 760 | | |
746 | 761 | | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
747 | 781 | | |
748 | 782 | | |
749 | 783 | | |
| |||
Lines changed: 54 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| |||
323 | 324 | | |
324 | 325 | | |
325 | 326 | | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
144 | 144 | | |
145 | 145 | | |
146 | 146 | | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
147 | 171 | | |
148 | 172 | | |
149 | 173 | | |
| |||
160 | 184 | | |
161 | 185 | | |
162 | 186 | | |
163 | | - | |
| 187 | + | |
| 188 | + | |
164 | 189 | | |
165 | 190 | | |
166 | 191 | | |
| |||
331 | 356 | | |
332 | 357 | | |
333 | 358 | | |
334 | | - | |
| 359 | + | |
| 360 | + | |
335 | 361 | | |
336 | 362 | | |
337 | 363 | | |
| |||
0 commit comments