[PR #5927] Store activeOrganizationSlug and activeOrganizationRole in Session by Default #14577

Open
opened 2026-04-13 09:31:59 -05:00 by GiteaMirror · 0 comments
Owner

Original Pull Request: https://github.com/better-auth/better-auth/pull/5927

State: open
Merged: No


Closes #5706

Summary

This PR adds activeOrganizationSlug and activeOrganizationRole to the session schema, enabling performance optimizations and improved developer experience when working with active organizations.

Changes

Schema Updates

  • Added activeOrganizationSlug and activeOrganizationRole fields to session schema (schema.ts, organization.ts)
  • Updated TypeScript types in middleware (call.ts)

Endpoint Updates

  • setActiveOrganization: Now populates slug and role when setting active organization
  • createOrganization: Sets slug and role in session when creating and activating new organization
  • updateOrganization: Updates session slug when active organization's slug changes
  • updateMemberRole: Updates session role when current user's role in active organization changes
  • removeMember / leaveOrganization: Clears session fields when user is removed/leaves active organization

Performance Optimizations

  • getActiveMemberRole: Uses session role when querying current user's active organization (avoids DB query)
  • hasPermission: Uses session role when checking permissions for current user's active organization (avoids DB query)

Testing

  • Added test cases for session updates on organization operations
  • Added performance optimization tests for getActiveMemberRole and hasPermission
  • Updated existing tests to verify session fields are properly maintained

Benefits

  1. Performance: Reduces database queries by using session data for common operations
  2. UX: Slug available immediately for URL routing without additional queries
  3. Developer Experience: Role and slug accessible directly from session
  4. Cookie Efficiency: Reduces need for separate API calls to fetch organization details

Migration

Run database migrations to add the new session fields:

npx @better-auth/cli@latest migrate

Breaking Changes

None - fields are optional and backward compatible.


Summary by cubic

Stores activeOrganizationSlug and activeOrganizationRole in the session and keeps them in sync across org actions, enabling faster permission checks and role lookups by avoiding extra DB queries.

  • New Features

    • Added activeOrganizationSlug and activeOrganizationRole to the session schema and middleware types.
    • Updated createOrganization, setActiveOrganization (by id or slug), updateOrganization (slug changes), updateMemberRole, removeMember, leaveOrganization, and delete to update or clear these session fields and cookies.
    • Optimized hasPermission and getActiveMemberRole to use session role when the active org matches, reducing database reads. When dynamicAccessControl is enabled, hasPermission falls back to DB lookups.
  • Migration

    • Run: npx @better-auth/cli@latest migrate to add the new session fields.

Written for commit 480942c6de. Summary will update automatically on new commits.

**Original Pull Request:** https://github.com/better-auth/better-auth/pull/5927 **State:** open **Merged:** No --- Closes #5706 ## Summary This PR adds `activeOrganizationSlug` and `activeOrganizationRole` to the session schema, enabling performance optimizations and improved developer experience when working with active organizations. ## Changes ### Schema Updates - Added `activeOrganizationSlug` and `activeOrganizationRole` fields to session schema (`schema.ts`, `organization.ts`) - Updated TypeScript types in middleware (`call.ts`) ### Endpoint Updates - **`setActiveOrganization`**: Now populates slug and role when setting active organization - **`createOrganization`**: Sets slug and role in session when creating and activating new organization - **`updateOrganization`**: Updates session slug when active organization's slug changes - **`updateMemberRole`**: Updates session role when current user's role in active organization changes - **`removeMember`** / **`leaveOrganization`**: Clears session fields when user is removed/leaves active organization ### Performance Optimizations - **`getActiveMemberRole`**: Uses session role when querying current user's active organization (avoids DB query) - **`hasPermission`**: Uses session role when checking permissions for current user's active organization (avoids DB query) ### Testing - Added test cases for session updates on organization operations - Added performance optimization tests for `getActiveMemberRole` and `hasPermission` - Updated existing tests to verify session fields are properly maintained ## Benefits 1. **Performance**: Reduces database queries by using session data for common operations 2. **UX**: Slug available immediately for URL routing without additional queries 3. **Developer Experience**: Role and slug accessible directly from session 4. **Cookie Efficiency**: Reduces need for separate API calls to fetch organization details ## Migration Run database migrations to add the new session fields: ```bash npx @better-auth/cli@latest migrate ``` ## Breaking Changes None - fields are optional and backward compatible. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Stores activeOrganizationSlug and activeOrganizationRole in the session and keeps them in sync across org actions, enabling faster permission checks and role lookups by avoiding extra DB queries. - **New Features** - Added activeOrganizationSlug and activeOrganizationRole to the session schema and middleware types. - Updated createOrganization, setActiveOrganization (by id or slug), updateOrganization (slug changes), updateMemberRole, removeMember, leaveOrganization, and delete to update or clear these session fields and cookies. - Optimized hasPermission and getActiveMemberRole to use session role when the active org matches, reducing database reads. When dynamicAccessControl is enabled, hasPermission falls back to DB lookups. - **Migration** - Run: npx @better-auth/cli@latest migrate to add the new session fields. <sup>Written for commit 480942c6deaa6513231d1dcc0b8b8c9983fc474e. Summary will update automatically on new commits.</sup> <!-- End of auto-generated description by cubic. -->
GiteaMirror added the pull-request label 2026-04-13 09:31:59 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#14577