PHPackages                             evo-mark/laravel-impersonate - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. evo-mark/laravel-impersonate

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

evo-mark/laravel-impersonate
============================

Laravel Impersonate is a plugin that allows to you to authenticate as your users.

v1.9.0(6mo ago)029MITPHPPHP ^8.2

Since Nov 3Pushed 6mo agoCompare

[ Source](https://github.com/evo-mark/laravel-impersonate)[ Packagist](https://packagist.org/packages/evo-mark/laravel-impersonate)[ RSS](/packages/evo-mark-laravel-impersonate/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (4)Versions (4)Used By (0)

 [    ![evoMark company logo](https://camo.githubusercontent.com/0ee00c831e18d82e5d32990e52b0b08b13c97ff2374478eef348c17bd3485c3c/68747470733a2f2f65766f6d61726b2e636f2e756b2f77702d636f6e74656e742f75706c6f6164732f7374617469632f65766f6d61726b2d6c6f676f2d2d6c696768742e737667)  ](https://evomark.co.uk)

 [ ![Build status](https://camo.githubusercontent.com/9bcce238edfbf70ba32bf83b2b8461a3c3df9f2c7c4ec7855da5f9c5d82d6347/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f65766f2d6d61726b2f6c61726176656c2d696d706572736f6e6174653f6c6f676f3d7061636b6167697374266c6f676f436f6c6f723d7768697465) ](https://packagist.org/packages/evo-mark/laravel-impersonate) [ ![Total Downloads](https://camo.githubusercontent.com/8ef81246e036d62d266c69d7eb3d2eea905514778539da35db52ffae01ee941d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f65766f2d6d61726b2f6c61726176656c2d696d706572736f6e617465) ](https://packagist.org/packages/evo-mark/laravel-impersonate) [ ![License](https://camo.githubusercontent.com/083433b68eedeee168c6a96691f000eeac9e01559f0e8401e0a11150e1acf53b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f65766f2d6d61726b2f6c61726176656c2d696d706572736f6e617465) ](https://packagist.org/packages/evo-mark/laravel-impersonate)

Laravel Impersonate
===================

[](#laravel-impersonate)

**Laravel Impersonate** makes it easy to **authenticate as your users**. Add a simple **trait** to your **user model** and impersonate as one of your users in one click.

- [Requirements](#requirements)
- [Installation](#installation)
- [Simple usage](#simple-usage)
    - [Using the built-in controller](#using-the-built-in-controller)
- [Advanced Usage](#advanced-usage)
    - [Defining impersonation authorization](#defining-impersonation-authorization)
    - [Using your own strategy](#using-your-own-strategy)
    - [Middleware](#middleware)
    - [Events](#events)
- [Configuration](#configuration)
- [Blade](#blade)
- [Tests](#tests)
- [Contributors](#contributors)
- [Why Not Just Use loginAsId()?](#rationale)

Requirements
------------

[](#requirements)

- Laravel 10.x to 11.x
- PHP &gt;= 8.2

### Laravel support

[](#laravel-support)

VersionRelease10.x to 11.x1.8Migrating to evo-mark/impersonate
---------------------------------

[](#migrating-to-evo-markimpersonate)

1. Remove the original package with `composer remove lab404/laravel-impersonate`
2. Install the forked version using the instructions below
3. Rename any uses of the package's classes in your app from `Lab404\Impersonate` to `EvoMark\Impersonate`

Installation
------------

[](#installation)

- Require it with Composer:

```
composer require evo-mark/laravel-impersonate
```

- Add the service provider at the end of your `config/app.php`:

```
'providers' => [
    // ...
    EvoMark\Impersonate\ImpersonateServiceProvider::class,
],
```

- Add the trait `EvoMark\Impersonate\Models\Impersonate` to your **User** model.

Simple usage
------------

[](#simple-usage)

Impersonate a user:

```
Auth::user()->impersonate($other_user);
// You're now logged as the $other_user
```

Leave impersonation:

```
Auth::user()->leaveImpersonation();
// You're now logged as your original user.
```

### Using the built-in controller

[](#using-the-built-in-controller)

In your routes file, under web middleware, you must call the `impersonate` route macro.

```
Route::impersonate();
```

Alternatively, you can execute this macro with your `RouteServiceProvider`.

```
namespace App\Providers;

class RouteServiceProvider extends ServiceProvider
{
    public function map() {
        Route::middleware('web')->group(function (Router $router) {
            $router->impersonate();
        });
    }
}
```

```
// Where $id is the ID of the user you want impersonate
route('impersonate', $id)

// Or in case of multi guards, you should also add `guardName` (defaults to `web`)
route('impersonate', ['id' => $id, 'guardName' => 'admin'])

// Generate an URL to leave current impersonation
route('impersonate.leave')
```

Advanced Usage
--------------

[](#advanced-usage)

### Defining impersonation authorization

[](#defining-impersonation-authorization)

By default all users can **impersonate** an user.
You need to add the method `canImpersonate()` to your user model:

```
    /**
     * @return bool
     */
    public function canImpersonate()
    {
        // For example
        return $this->is_admin == 1;
    }
```

By default all users can **be impersonated**.
You need to add the method `canBeImpersonated()` to your user model to extend this behavior:

```
    /**
     * @return bool
     */
    public function canBeImpersonated()
    {
        // For example
        return $this->can_be_impersonated == 1;
    }
```

### Using your own strategy

[](#using-your-own-strategy)

- Getting the manager:

```
// With the app helper
app('impersonate')
// Dependency Injection
public function impersonate(ImpersonateManager $manager, $user_id) { /* ... */ }
```

- Working with the manager:

```
$manager = app('impersonate');

// Find a user by their ID
$manager->findUserById($id);

// TRUE if your are impersonating an user.
$manager->isImpersonating();

// Impersonate an user. Pass the original user and the user you want to impersonate
$manager->take($from, $to);

// Leave current impersonation
$manager->leave();

// Get the impersonator ID
$manager->getImpersonatorId();
```

### Middleware

[](#middleware)

**Protect From Impersonation**

You can use the middleware `impersonate.protect` to protect your routes against user impersonation.
This middleware can be useful when you want to protect specific pages like users subscriptions, users credit cards, ...

```
Router::get('/my-credit-card', function() {
    echo "Can't be accessed by an impersonator";
})->middleware('impersonate.protect');
```

### Events

[](#events)

There are two events available that can be used to improve your workflow:

- `TakeImpersonation` is fired when an impersonation is taken.
- `LeaveImpersonation` is fired when an impersonation is leaved.

Each events returns two properties `$event->impersonator` and `$event->impersonated` containing User model instance.

Configuration
-------------

[](#configuration)

The package comes with a configuration file.

Publish it with the following command:

```
php artisan vendor:publish --tag=impersonate
```

Available options:

```
    // The session key used to store the original user id.
    'session_key' => 'impersonated_by',
    // Where to redirect after taking an impersonation.
    // Only used in the built-in controller.
    // You can use: an URI, the keyword back (to redirect back) or a route name
    'take_redirect_to' => '/',
    // Where to redirect after leaving an impersonation.
    // Only used in the built-in controller.
    // You can use: an URI, the keyword back (to redirect back) or a route name
    'leave_redirect_to' => '/'
```

Blade
-----

[](#blade)

There are three Blade directives available.

### When the user can impersonate

[](#when-the-user-can-impersonate)

```
@canImpersonate($guard = null)
    Impersonate this user
@endCanImpersonate
```

### When the user can be impersonated

[](#when-the-user-can-be-impersonated)

This comes in handy when you have a user list and want to show an "Impersonate" button next to all the users. But you don't want that button next to the current authenticated user neither to that users which should not be able to impersonated according your implementation of `canBeImpersonated()` .

```
@canBeImpersonated($user, $guard = null)
    Impersonate this user
@endCanBeImpersonated
```

### When the user is impersonated

[](#when-the-user-is-impersonated)

```
@impersonating($guard = null)
    Leave impersonation
@endImpersonating
```

Tests
-----

[](#tests)

```
vendor/bin/phpunit
```

Contributors
------------

[](#contributors)

- This package was forked from [Lab404](https://github.com/404labfr/laravel-impersonate) and is maintained by evoMark.

Rationale
---------

[](#rationale)

### Why not just use `loginAsId()`?

[](#why-not-just-use-loginasid)

This package adds broader functionality, including Blade directives to allow you to override analytics and other tracking events when impersonating, fire events based on impersonation status, and more. Brief discussion at [issues/5](https://github.com/404labfr/laravel-impersonate/issues/5)

### Why did you fork this package

[](#why-did-you-fork-this-package)

The original package seems to be in maintenance now, only receiving minor updates without addressing bugs or features. We wanted a few of those features.

Licence
-------

[](#licence)

MIT

###  Health Score

38

—

LowBetter than 85% of packages

Maintenance66

Regular maintenance activity

Popularity9

Limited adoption so far

Community19

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 56.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~176 days

Total

3

Last Release

204d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/4f42ee39ec10e8f4b3552a78ea78628acef13e9772bed852baa186e97bde7748?d=identicon)[evomark](/maintainers/evomark)

---

Top Contributors

[![MarceauKa](https://avatars.githubusercontent.com/u/1665333?v=4)](https://github.com/MarceauKa "MarceauKa (88 commits)")[![tghpow](https://avatars.githubusercontent.com/u/6094280?v=4)](https://github.com/tghpow "tghpow (15 commits)")[![pascalbaljet](https://avatars.githubusercontent.com/u/8403149?v=4)](https://github.com/pascalbaljet "pascalbaljet (7 commits)")[![Konafets](https://avatars.githubusercontent.com/u/363363?v=4)](https://github.com/Konafets "Konafets (5 commits)")[![ctf0](https://avatars.githubusercontent.com/u/7388088?v=4)](https://github.com/ctf0 "ctf0 (5 commits)")[![mattdfloyd](https://avatars.githubusercontent.com/u/185187?v=4)](https://github.com/mattdfloyd "mattdfloyd (5 commits)")[![craigrileyuk](https://avatars.githubusercontent.com/u/68274157?v=4)](https://github.com/craigrileyuk "craigrileyuk (4 commits)")[![riekelt](https://avatars.githubusercontent.com/u/1822200?v=4)](https://github.com/riekelt "riekelt (2 commits)")[![mharacewiat](https://avatars.githubusercontent.com/u/5583430?v=4)](https://github.com/mharacewiat "mharacewiat (2 commits)")[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (2 commits)")[![carsso](https://avatars.githubusercontent.com/u/666182?v=4)](https://github.com/carsso "carsso (2 commits)")[![lloricode](https://avatars.githubusercontent.com/u/8251344?v=4)](https://github.com/lloricode "lloricode (1 commits)")[![matiullah31](https://avatars.githubusercontent.com/u/9604691?v=4)](https://github.com/matiullah31 "matiullah31 (1 commits)")[![nenads](https://avatars.githubusercontent.com/u/1824176?v=4)](https://github.com/nenads "nenads (1 commits)")[![ryangjchandler](https://avatars.githubusercontent.com/u/41837763?v=4)](https://github.com/ryangjchandler "ryangjchandler (1 commits)")[![Smart-S-Max](https://avatars.githubusercontent.com/u/59285441?v=4)](https://github.com/Smart-S-Max "Smart-S-Max (1 commits)")[![svenluijten](https://avatars.githubusercontent.com/u/11269635?v=4)](https://github.com/svenluijten "svenluijten (1 commits)")[![bmichotte](https://avatars.githubusercontent.com/u/235510?v=4)](https://github.com/bmichotte "bmichotte (1 commits)")[![Vendin](https://avatars.githubusercontent.com/u/9011975?v=4)](https://github.com/Vendin "Vendin (1 commits)")[![drbyte](https://avatars.githubusercontent.com/u/404472?v=4)](https://github.com/drbyte "drbyte (1 commits)")

---

Tags

pluginlaravelpackageauthuserimpersonationimpersonatelaravel-packagelaravel-plugin

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/evo-mark-laravel-impersonate/health.svg)

```
[![Health](https://phpackages.com/badges/evo-mark-laravel-impersonate/health.svg)](https://phpackages.com/packages/evo-mark-laravel-impersonate)
```

###  Alternatives

[lab404/laravel-impersonate

Laravel Impersonate is a plugin that allows to you to authenticate as your users.

2.3k16.4M48](/packages/lab404-laravel-impersonate)[rickycezar/laravel-jwt-impersonate

Laravel Impersonate is a plugin that allows to you to authenticate as your users.

24117.6k](/packages/rickycezar-laravel-jwt-impersonate)[hapidjus/laravel-impersonate-ui

UI for 404labfr/laravel-impersonate

371.5k](/packages/hapidjus-laravel-impersonate-ui)[lab404/laravel-auth-checker

Laravel Auth Checker allows you to log users authentication, devices authenticated from and lock intrusions.

223164.9k2](/packages/lab404-laravel-auth-checker)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
