Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Impersonate

Impersonate Laravel Package

j84115/impersonate

Simple Laravel package to temporarily impersonate other users via /impersonate/login/{user_id} and /impersonate/logout. Add service provider, implement ImpersonateUser on your User model to define who can impersonate and who can be impersonated, then register Route::impersonate().

View on GitHub
Deep Wiki
Context7

A simple Laravel Package to temporarily login as other users.

Frequently asked questions about Impersonate
How do I install j84115/impersonate since it’s not on Packagist yet?
Since the package isn’t published to Packagist, manually install it by adding the GitHub repository to your `composer.json` under `repositories`, then run `composer require j84115/impersonate`. Replace the placeholder URL with `https://github.com/84115/Laravel-Impersonate`.
Which Laravel versions does this package support?
The package is designed for Laravel’s LTS versions (8.x and 10.x) and assumes compatibility with PHP 8.0+. Check the GitHub repository for updates, as explicit version constraints may not be documented yet. Test thoroughly if using older Laravel versions.
Can I restrict impersonation to specific roles or attributes?
Yes, implement the `ImpersonateUser` interface on your `User` model and define `impersonator()` (who can impersonate) and `impersonatable()` (who can be impersonated) methods. For example, restrict admins only: `return $this->role === 'admin';` for `impersonator()`.
Does this package work with Laravel Sanctum or Passport for API impersonation?
No, this package is built for session-based Laravel auth (web middleware) and may not integrate cleanly with Sanctum/Passport. It relies on session manipulation, which isn’t directly compatible with token-based auth. Consider custom middleware for API impersonation.
How do I secure the impersonate routes from brute-force attacks?
The package doesn’t include rate limiting by default, so add Laravel’s `throttle` middleware to the `/impersonate/login` route. Example: `Route::middleware(['throttle:5,1'])->impersonate()`. Also, log impersonation events for audit trails.
What happens if the impersonated user is already logged in?
The package replaces the current session with the impersonated user’s session. If the target user is active, their session may be overwritten, potentially logging them out. Test this behavior in your environment to ensure it aligns with your app’s requirements.
Can I use this with custom session drivers like Redis or database?
Yes, but be aware of potential issues with session persistence across load balancers or clustered environments. The package manipulates sessions, so ensure your session driver supports session hijacking without data loss. Test thoroughly with your specific setup.
How do I add impersonation to my existing auth middleware?
Use the `Route::impersonate()` macro in your `routes/web.php` and guard it with Laravel’s auth middleware. Example: `Route::middleware(['auth'])->impersonate()`. This ensures only authenticated users can access impersonation routes.
Are there alternatives to this package for Laravel impersonation?
Yes, alternatives include `spatie/laravel-activitylog` (for auditing) combined with custom middleware, or `laravel-permission` packages that offer role-based impersonation. For a dedicated solution, `gloudemans/impersonate` is another popular option with more features like audit logging.
How do I test impersonation functionality in my Laravel app?
Use Laravel’s testing tools to simulate impersonation. Mock the `ImpersonateUser` interface in tests and assert session changes. Example: `actingAs($admin)->get('/impersonate/login/1')->assertRedirect('/')`. Verify the impersonated user’s session is active and can be terminated via `/impersonate/logout`.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
besmartand-pro/php-quality-config
sentix/ai-chatbot
terminal42/code-quality-tools
codifyo/ts-generator-bundle
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity