[PR #7528] [CLOSED] feat: Add organization role lifecycle hooks with additional field filtering #7377

Closed
opened 2026-03-13 13:34:00 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/7528
Author: @UdaraWanasinghe
Created: 1/21/2026
Status: Closed

Base: canaryHead: canary


📝 Commits (10+)

  • 0a9b050 feat: add organization role hooks for create, update, and delete operations
  • ae77a96 feat: add organization role hooks for create, update, and delete operations
  • f016187 feat: add role hooks for organization lifecycle operations (create, update, delete)
  • 5c2ff20 feat: filter additional fields in create and update organization role functions
  • cc6d8f3 feat: re-validate permissions in create and update organization role functions
  • dd5da12 feat: ensure undeclared fields are not persisted in role data
  • 6e2fab8 feat: add error handling for non-existent organization in deleteOrgRole function
  • a447276 feat: add organization existence checks and error logging in role management functions
  • ddc60b5 fix: update type definition for afterUpdateRole callback to ensure role is not null
  • 37f66db feat: re-validate role name in create and update organization role functions if modified

📊 Changes

6 files changed (+855 additions, -9 deletions)

View changed files

📝 docs/content/docs/plugins/organization.mdx (+51 -0)
📝 packages/better-auth/src/plugins/organization/routes/crud-access-control.test.ts (+427 -1)
📝 packages/better-auth/src/plugins/organization/routes/crud-access-control.ts (+266 -8)
📝 packages/better-auth/src/plugins/organization/types.ts (+75 -0)
packages/better-auth/src/utils/deep-equal.ts (+35 -0)
📝 packages/better-auth/src/utils/index.ts (+1 -0)

📄 Description

This PR introduces new hooks for controlling organization role lifecycle operations (create, update, delete) in the Better Auth organization plugin. These hooks allow developers to customize role management behavior, validate changes, and perform side effects during role operations.

Key Changes

  • New Hooks Added:

    • beforeCreateRole: Intercept and modify role creation data before saving.
    • afterCreateRole: Execute actions after a role is created.
    • beforeUpdateRole: Validate or alter updates to existing roles.
    • afterUpdateRole: Perform post-update logic.
    • beforeDeleteRole: Prevent deletion or run pre-deletion checks.
    • afterDeleteRole: Handle cleanup after role deletion.
  • Additional Field Filtering: Enhanced security and stability by filtering hook-returned data to only include fields declared in schema.organizationRole.additionalFields. This prevents unintended or undeclared fields from being persisted to the database.

  • Documentation Update: Added comprehensive examples and usage notes for the new role hooks in the organization plugin documentation.

Usage Example

organization({
  organizationHooks: {
    beforeCreateRole: async ({ role, organization, user }) => {
      // Validate or modify role data
      return { data: { ...role, customField: "value" } };
    },
    // ... other hooks
  },
})

Notes

  • Hooks receive context including the role, organization, user, and (for updates) the proposed changes.
  • before* hooks can return { data: { ... } } to merge changes into the operation payload.
  • Additional fields must be explicitly declared in the schema to be persisted.
  • This change maintains type safety, follows existing code conventions, and aims for API stability by only allowing declared fields through hooks.

This feature enhances the organization plugin's flexibility while ensuring data integrity. No breaking changes to existing APIs.


Summary by cubic

Adds lifecycle hooks for organization roles (create, update, delete), re-validates permissions and role names when hooks modify them, and filters hook-returned fields to only schema-declared additional fields. Ensures the organization exists during hook runs, letting apps customize role logic safely without persisting undeclared data.

  • New Features
    • Role hooks: beforeCreateRole, afterCreateRole, beforeUpdateRole, afterUpdateRole, beforeDeleteRole, afterDeleteRole.
    • Hooks receive role, organization, and user. before* hooks can return { data } to modify payloads; permissions and role name are re-validated if changed, and organization existence is enforced.
    • Only keys declared in schema.organizationRole.additionalFields are persisted; undeclared keys are ignored.
    • Docs updated with examples. Tests cover all hooks.

Written for commit 075768019b. Summary will update on new commits.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/7528 **Author:** [@UdaraWanasinghe](https://github.com/UdaraWanasinghe) **Created:** 1/21/2026 **Status:** ❌ Closed **Base:** `canary` ← **Head:** `canary` --- ### 📝 Commits (10+) - [`0a9b050`](https://github.com/better-auth/better-auth/commit/0a9b0508480ac22934a8fee3bd6774210b4bd331) feat: add organization role hooks for create, update, and delete operations - [`ae77a96`](https://github.com/better-auth/better-auth/commit/ae77a96aacb2ea72a7b66bd9b6e907c1b29f22ea) feat: add organization role hooks for create, update, and delete operations - [`f016187`](https://github.com/better-auth/better-auth/commit/f0161874f9755eeb29d5834d5dc3942d605d790a) feat: add role hooks for organization lifecycle operations (create, update, delete) - [`5c2ff20`](https://github.com/better-auth/better-auth/commit/5c2ff20905923c302c87e737f98032d73ac09e55) feat: filter additional fields in create and update organization role functions - [`cc6d8f3`](https://github.com/better-auth/better-auth/commit/cc6d8f3430fd06769ca2c496640a2ce2875360af) feat: re-validate permissions in create and update organization role functions - [`dd5da12`](https://github.com/better-auth/better-auth/commit/dd5da12367f6f1010fc7c5810f9c19509b29ada3) feat: ensure undeclared fields are not persisted in role data - [`6e2fab8`](https://github.com/better-auth/better-auth/commit/6e2fab8aba77f20d044ea89ed479992f11344e8d) feat: add error handling for non-existent organization in deleteOrgRole function - [`a447276`](https://github.com/better-auth/better-auth/commit/a447276f91ec3198d4c727bf4f97083847fd65c1) feat: add organization existence checks and error logging in role management functions - [`ddc60b5`](https://github.com/better-auth/better-auth/commit/ddc60b59f5a5733df2310fab5d437028dc383cf1) fix: update type definition for afterUpdateRole callback to ensure role is not null - [`37f66db`](https://github.com/better-auth/better-auth/commit/37f66db69055d96c2c2b9b384c028bbddb77b8c7) feat: re-validate role name in create and update organization role functions if modified ### 📊 Changes **6 files changed** (+855 additions, -9 deletions) <details> <summary>View changed files</summary> 📝 `docs/content/docs/plugins/organization.mdx` (+51 -0) 📝 `packages/better-auth/src/plugins/organization/routes/crud-access-control.test.ts` (+427 -1) 📝 `packages/better-auth/src/plugins/organization/routes/crud-access-control.ts` (+266 -8) 📝 `packages/better-auth/src/plugins/organization/types.ts` (+75 -0) ➕ `packages/better-auth/src/utils/deep-equal.ts` (+35 -0) 📝 `packages/better-auth/src/utils/index.ts` (+1 -0) </details> ### 📄 Description This PR introduces new hooks for controlling organization role lifecycle operations (create, update, delete) in the Better Auth organization plugin. These hooks allow developers to customize role management behavior, validate changes, and perform side effects during role operations. #### Key Changes - **New Hooks Added**: - `beforeCreateRole`: Intercept and modify role creation data before saving. - `afterCreateRole`: Execute actions after a role is created. - `beforeUpdateRole`: Validate or alter updates to existing roles. - `afterUpdateRole`: Perform post-update logic. - `beforeDeleteRole`: Prevent deletion or run pre-deletion checks. - `afterDeleteRole`: Handle cleanup after role deletion. - **Additional Field Filtering**: Enhanced security and stability by filtering hook-returned data to only include fields declared in `schema.organizationRole.additionalFields`. This prevents unintended or undeclared fields from being persisted to the database. - **Documentation Update**: Added comprehensive examples and usage notes for the new role hooks in the organization plugin documentation. #### Usage Example ```typescript organization({ organizationHooks: { beforeCreateRole: async ({ role, organization, user }) => { // Validate or modify role data return { data: { ...role, customField: "value" } }; }, // ... other hooks }, }) ``` #### Notes - Hooks receive context including the role, organization, user, and (for updates) the proposed changes. - `before*` hooks can return `{ data: { ... } }` to merge changes into the operation payload. - Additional fields must be explicitly declared in the schema to be persisted. - This change maintains type safety, follows existing code conventions, and aims for API stability by only allowing declared fields through hooks. This feature enhances the organization plugin's flexibility while ensuring data integrity. No breaking changes to existing APIs. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Adds lifecycle hooks for organization roles (create, update, delete), re-validates permissions and role names when hooks modify them, and filters hook-returned fields to only schema-declared additional fields. Ensures the organization exists during hook runs, letting apps customize role logic safely without persisting undeclared data. - **New Features** - Role hooks: beforeCreateRole, afterCreateRole, beforeUpdateRole, afterUpdateRole, beforeDeleteRole, afterDeleteRole. - Hooks receive role, organization, and user. before* hooks can return { data } to modify payloads; permissions and role name are re-validated if changed, and organization existence is enforced. - Only keys declared in schema.organizationRole.additionalFields are persisted; undeclared keys are ignored. - Docs updated with examples. Tests cover all hooks. <sup>Written for commit 075768019b57015c0c9492c9ef250922566093f4. Summary will update on new commits.</sup> <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-03-13 13:34:01 -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#7377