[PR #5599] Implement beforeLeave and afterLeave hooks for organization #23008

Open
opened 2026-04-15 21:25:09 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/5599
Author: @GautamBytes
Created: 10/26/2025
Status: 🔄 Open

Base: mainHead: feat/org-leave-hooks


📝 Commits (6)

  • 9e99772 add before/afterLeaveOrganization hooks
  • 848ac35 refactor(org): reuse removeMember hooks for leave
  • 28da5dc Pass request context to member hooks
  • cafe7fa add try-catch to removeMember and improve error messages
  • e22b672 fix: add optional chaining for ctx.request in examples
  • 2399964 lint fix

📊 Changes

4 files changed (+398 additions, -41 deletions)

View changed files

📝 docs/content/docs/plugins/organization.mdx (+13 -2)
📝 packages/better-auth/src/plugins/organization/routes/crud-members.test.ts (+258 -2)
📝 packages/better-auth/src/plugins/organization/routes/crud-members.ts (+110 -26)
📝 packages/better-auth/src/plugins/organization/types.ts (+17 -11)

📄 Description

Closes #5435

This PR introduces two new lifecycle hooks to the organization plugin: beforeLeaveOrganization and afterLeaveOrganization.

This allows developers to run custom logic, such as cleaning up user-related data or sending notifications, when a user voluntarily leaves an organization.

Implementation Details

  • crud-members.ts: The leaveOrganization endpoint now wraps the deletion logic in a try...catch block.
    • beforeLeaveOrganization is called before the member is deleted. If this hook throws an error, the entire operation is aborted, and the user is not removed.
    • afterLeaveOrganization is called after the member has been successfully deleted.
  • organization/types.ts: Added the new hook definitions to the OrganizationHooks interface.
  • organization.mdx: Added documentation and example usage for the new hooks.
  • crud-members.test.ts: Added new tests to verify:
    1. Both hooks are called in the correct order on a successful leave.
    2. If beforeLeaveOrganization throws an error, the member is not deleted, and afterLeaveOrganization is not called.

Summary by cubic

Make leaveOrganization run the existing beforeRemoveMember and afterRemoveMember hooks, and pass ctx so you can tell self-leave vs admin removal. Leaving is blocked if beforeRemoveMember throws; afterRemoveMember runs only on success.

  • New Features
    • Hooks now receive ctx in OrganizationHooks for beforeRemoveMember and afterRemoveMember.
    • leaveOrganization calls these hooks with member, user, organization; aborts on error from beforeRemoveMember.
    • Updated docs and tests for success, order, failure, and context differentiation.

Written for commit 2399964611. Summary will update automatically 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/5599 **Author:** [@GautamBytes](https://github.com/GautamBytes) **Created:** 10/26/2025 **Status:** 🔄 Open **Base:** `main` ← **Head:** `feat/org-leave-hooks` --- ### 📝 Commits (6) - [`9e99772`](https://github.com/better-auth/better-auth/commit/9e997729035d44348ea2bb4a229a03f17533e461) add before/afterLeaveOrganization hooks - [`848ac35`](https://github.com/better-auth/better-auth/commit/848ac35c57e0e64ed94f7e8f42ff6017dea3dd6c) refactor(org): reuse removeMember hooks for leave - [`28da5dc`](https://github.com/better-auth/better-auth/commit/28da5dc91d82e5f615e78c63073151caf4020379) Pass request context to member hooks - [`cafe7fa`](https://github.com/better-auth/better-auth/commit/cafe7fab29330c86e5e1fd06cfa44e63556f29dc) add try-catch to removeMember and improve error messages - [`e22b672`](https://github.com/better-auth/better-auth/commit/e22b6721d7c1055a13b93cf13bc14b88aa0cb34f) fix: add optional chaining for ctx.request in examples - [`2399964`](https://github.com/better-auth/better-auth/commit/23999646110f5424124909344b122103814ff8cc) lint fix ### 📊 Changes **4 files changed** (+398 additions, -41 deletions) <details> <summary>View changed files</summary> 📝 `docs/content/docs/plugins/organization.mdx` (+13 -2) 📝 `packages/better-auth/src/plugins/organization/routes/crud-members.test.ts` (+258 -2) 📝 `packages/better-auth/src/plugins/organization/routes/crud-members.ts` (+110 -26) 📝 `packages/better-auth/src/plugins/organization/types.ts` (+17 -11) </details> ### 📄 Description Closes #5435 This PR introduces two new lifecycle hooks to the organization plugin: `beforeLeaveOrganization` and `afterLeaveOrganization`. This allows developers to run custom logic, such as cleaning up user-related data or sending notifications, when a user voluntarily leaves an organization. #### Implementation Details * **`crud-members.ts`**: The `leaveOrganization` endpoint now wraps the deletion logic in a `try...catch` block. * `beforeLeaveOrganization` is called *before* the member is deleted. If this hook throws an error, the entire operation is aborted, and the user is not removed. * `afterLeaveOrganization` is called *after* the member has been successfully deleted. * **`organization/types.ts`**: Added the new hook definitions to the `OrganizationHooks` interface. * **`organization.mdx`**: Added documentation and example usage for the new hooks. * **`crud-members.test.ts`**: Added new tests to verify: 1. Both hooks are called in the correct order on a successful leave. 2. If `beforeLeaveOrganization` throws an error, the member is **not** deleted, and `afterLeaveOrganization` is **not** called. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Make leaveOrganization run the existing beforeRemoveMember and afterRemoveMember hooks, and pass ctx so you can tell self-leave vs admin removal. Leaving is blocked if beforeRemoveMember throws; afterRemoveMember runs only on success. - **New Features** - Hooks now receive ctx in OrganizationHooks for beforeRemoveMember and afterRemoveMember. - leaveOrganization calls these hooks with member, user, organization; aborts on error from beforeRemoveMember. - Updated docs and tests for success, order, failure, and context differentiation. <sup>Written for commit 23999646110f5424124909344b122103814ff8cc. Summary will update automatically 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-04-15 21:25:09 -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#23008