[PR #5542] [CLOSED] Add onImpersonationStart and onImpersonationStop hooks #31672

Closed
opened 2026-04-17 22:33:46 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/5542
Author: @GautamBytes
Created: 10/24/2025
Status: Closed

Base: canaryHead: feat/impersonation-hooks


📝 Commits (2)

  • b6410bc add onImpersonationStart and onImpersonationStop hooks
  • 4de31b2 Adjust session count expectation in revoke user session test

📊 Changes

3 files changed (+99 additions, -5 deletions)

View changed files

📝 packages/better-auth/src/plugins/admin/admin.test.ts (+54 -5)
📝 packages/better-auth/src/plugins/admin/admin.ts (+31 -0)
📝 packages/better-auth/src/plugins/admin/types.ts (+14 -0)

📄 Description

Resolves #5444

Description

This PR introduces two new optional configuration hooks to the admin plugin, as requested in issue #5444:

  • onImpersonationStart(data)
  • onImpersonationStop(data)

These hooks allow developers to easily implement audit logging or other side effects when an admin starts or stops an impersonation session.

The data object passed to both hooks contains:

  • adminUserId: string
  • impersonatedUserId: string

Key Features

  • Async Support: Both hooks are async-aware and will be properly awaited.
  • Robust Error Handling: Any errors thrown inside a hook are caught and logged without breaking the core impersonation or stop-impersonation flow.
  • Fully Tested: Includes updated tests to verify the hooks are called correctly and with the proper data, ensuring tests are independent and can be run in parallel.

Summary by cubic

Adds two optional hooks to the admin plugin to run code when an admin starts or stops impersonating a user. Hooks are awaited, and failures are logged without interrupting impersonation.

  • New Features
    • onImpersonationStart({ adminUserId, impersonatedUserId }) and onImpersonationStop({ adminUserId, impersonatedUserId }).
    • Async-aware; errors inside hooks are caught and logged, core flow continues.
    • Tests verify both hooks fire with the correct payloads.

🔄 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/5542 **Author:** [@GautamBytes](https://github.com/GautamBytes) **Created:** 10/24/2025 **Status:** ❌ Closed **Base:** `canary` ← **Head:** `feat/impersonation-hooks` --- ### 📝 Commits (2) - [`b6410bc`](https://github.com/better-auth/better-auth/commit/b6410bce2d2e41664849b5e58bc8b6b7db35d26f) add onImpersonationStart and onImpersonationStop hooks - [`4de31b2`](https://github.com/better-auth/better-auth/commit/4de31b2eb20dbbc3606348cb8dd9adf7c486647d) Adjust session count expectation in revoke user session test ### 📊 Changes **3 files changed** (+99 additions, -5 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/plugins/admin/admin.test.ts` (+54 -5) 📝 `packages/better-auth/src/plugins/admin/admin.ts` (+31 -0) 📝 `packages/better-auth/src/plugins/admin/types.ts` (+14 -0) </details> ### 📄 Description Resolves #5444 ## Description This PR introduces two new optional configuration hooks to the `admin` plugin, as requested in issue #5444: - `onImpersonationStart(data)` - `onImpersonationStop(data)` These hooks allow developers to easily implement audit logging or other side effects when an admin starts or stops an impersonation session. The `data` object passed to both hooks contains: - `adminUserId: string` - `impersonatedUserId: string` ## Key Features - **Async Support:** Both hooks are `async`-aware and will be properly awaited. - **Robust Error Handling:** Any errors thrown inside a hook are caught and logged without breaking the core impersonation or stop-impersonation flow. - **Fully Tested:** Includes updated tests to verify the hooks are called correctly and with the proper data, ensuring tests are independent and can be run in parallel. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Adds two optional hooks to the admin plugin to run code when an admin starts or stops impersonating a user. Hooks are awaited, and failures are logged without interrupting impersonation. - **New Features** - onImpersonationStart({ adminUserId, impersonatedUserId }) and onImpersonationStop({ adminUserId, impersonatedUserId }). - Async-aware; errors inside hooks are caught and logged, core flow continues. - Tests verify both hooks fire with the correct payloads. <!-- 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-17 22:33:47 -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#31672