[PR #3480] [CLOSED] feat(organization): add beforeAccept and afterAccept hooks for invitation acceptance #21749

Closed
opened 2026-04-15 20:34:27 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/3480
Author: @nokwin
Created: 7/19/2025
Status: Closed

Base: mainHead: feat/accept-invitation-hooks


📝 Commits (2)

  • aa93226 feat(organization): add beforeAccept and afterAccept hooks for invitation acceptance
  • 8a8db1d Merge branch 'main' into feat/accept-invitation-hooks

📊 Changes

4 files changed (+563 additions, -1 deletions)

View changed files

📝 docs/content/docs/plugins/organization.mdx (+45 -0)
📝 packages/better-auth/src/plugins/organization/organization.test.ts (+453 -0)
📝 packages/better-auth/src/plugins/organization/routes/crud-invites.ts (+40 -0)
📝 packages/better-auth/src/plugins/organization/types.ts (+25 -1)

📄 Description

What?

Added beforeAccept and afterAccept hooks for organization invitation acceptance, allowing developers to customize the invitation acceptance process with custom logic that runs before and after an invitation is accepted.

Why?

Organizations need flexibility to implement custom business logic during invitation acceptance, such as:

  • Validating user permissions before accepting admin invitations
  • Sending welcome emails or creating default resources after acceptance
  • Implementing custom audit logging
  • Integrating with external systems
  • Adding organization-specific validation rules

How?

  • Added acceptingInvitation configuration option with beforeAccept and afterAccept hooks
  • Implemented hook execution in the acceptInvitation route with proper error handling
  • Added organization validation before hook execution
  • Updated TypeScript types to support hook parameters and return values
  • Added comprehensive documentation with usage examples

Example usage:

organization({
  acceptingInvitation: {
    beforeAccept: async ({ invitation, user }) => {
      await verifyInvitationRules(invitation.id, user.id)
    },
    afterAccept: async ({ invitation, member, user }) => {
      await sendWelcomeEmail(user.email, invitation.organization.name)
      await setupDefaultUserResources(member.id)
    }
  }
})

Summary by cubic

Added beforeAccept and afterAccept hooks to the organization invitation acceptance flow, allowing custom logic to run before and after a user accepts an invitation.

  • New Features
    • Introduced acceptingInvitation config with beforeAccept and afterAccept hooks.
    • Hooks receive invitation, user, and organization data, and can handle errors.
    • Updated documentation with usage examples.
    • Added tests covering hook behavior and error cases.

🔄 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/3480 **Author:** [@nokwin](https://github.com/nokwin) **Created:** 7/19/2025 **Status:** ❌ Closed **Base:** `main` ← **Head:** `feat/accept-invitation-hooks` --- ### 📝 Commits (2) - [`aa93226`](https://github.com/better-auth/better-auth/commit/aa9322677bbc643320f51fc3d68ac2df9baf9ac7) feat(organization): add beforeAccept and afterAccept hooks for invitation acceptance - [`8a8db1d`](https://github.com/better-auth/better-auth/commit/8a8db1d7dd2cca5bfd120d62c5a544b46edf1b63) Merge branch 'main' into feat/accept-invitation-hooks ### 📊 Changes **4 files changed** (+563 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `docs/content/docs/plugins/organization.mdx` (+45 -0) 📝 `packages/better-auth/src/plugins/organization/organization.test.ts` (+453 -0) 📝 `packages/better-auth/src/plugins/organization/routes/crud-invites.ts` (+40 -0) 📝 `packages/better-auth/src/plugins/organization/types.ts` (+25 -1) </details> ### 📄 Description ## What? Added `beforeAccept` and `afterAccept` hooks for organization invitation acceptance, allowing developers to customize the invitation acceptance process with custom logic that runs before and after an invitation is accepted. ## Why? Organizations need flexibility to implement custom business logic during invitation acceptance, such as: - Validating user permissions before accepting admin invitations - Sending welcome emails or creating default resources after acceptance - Implementing custom audit logging - Integrating with external systems - Adding organization-specific validation rules ## How? - Added `acceptingInvitation` configuration option with `beforeAccept` and `afterAccept` hooks - Implemented hook execution in the `acceptInvitation` route with proper error handling - Added organization validation before hook execution - Updated TypeScript types to support hook parameters and return values - Added comprehensive documentation with usage examples **Example usage:** ```typescript organization({ acceptingInvitation: { beforeAccept: async ({ invitation, user }) => { await verifyInvitationRules(invitation.id, user.id) }, afterAccept: async ({ invitation, member, user }) => { await sendWelcomeEmail(user.email, invitation.organization.name) await setupDefaultUserResources(member.id) } } }) ``` <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Added beforeAccept and afterAccept hooks to the organization invitation acceptance flow, allowing custom logic to run before and after a user accepts an invitation. - **New Features** - Introduced acceptingInvitation config with beforeAccept and afterAccept hooks. - Hooks receive invitation, user, and organization data, and can handle errors. - Updated documentation with usage examples. - Added tests covering hook behavior and error cases. <!-- 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 20:34:27 -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#21749