PHPackages                             bioman1464/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. bioman1464/laravel-impersonate

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

bioman1464/laravel-impersonate
==============================

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

1.7.7(1y ago)08.1k↓100%MITPHPPHP ^7.2 | ^8.0

Since Oct 6Pushed 1y agoCompare

[ Source](https://github.com/Bioman1464/laravel-impersonate)[ Packagist](https://packagist.org/packages/bioman1464/laravel-impersonate)[ RSS](/packages/bioman1464-laravel-impersonate/feed)WikiDiscussions master Synced today

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

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

[](#laravel-impersonate)

[![Build Status](https://camo.githubusercontent.com/27c9519f0321ff6cc06bbe75c749f3763cf0f6bd65a11f2ed650771be204b489/68747470733a2f2f7472617669732d63692e6f72672f3430346c616266722f6c61726176656c2d696d706572736f6e6174652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/404labfr/laravel-impersonate) [![Scrutinizer Code Quality](https://camo.githubusercontent.com/95cdce44f577e92ec8767d3fcf667e0a6748a5c22a270e0fff30263525500345/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f3430346c616266722f6c61726176656c2d696d706572736f6e6174652f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/404labfr/laravel-impersonate/?branch=master)

**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 6.x to 11.x
- PHP &gt;= 7.2 or &gt;= 8.0

### Laravel support

[](#laravel-support)

VersionRelease6.x to 11.x1.76.x, 7.x1.65.81.55.7, 5.61.25.5, 5.41.1Installation
------------

[](#installation)

- Require it with Composer:

```
composer require lab404/laravel-impersonate
```

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

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

- Add the trait `Lab404\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 an user by its 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 created by [MarceauKa](https://github.com/MarceauKa) and [tghpow](https://github.com/tghpow). Many thanks to all of our [contributors](https://github.com/404labfr/laravel-impersonate/graphs/contributors).

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)

Licence
-------

[](#licence)

MIT

###  Health Score

34

—

LowBetter than 79% of packages

Maintenance32

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community19

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 56.4% 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 ~187 days

Total

4

Last Release

700d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/925922594475d5b96de1c3ae29fbc41dce6b79e8f4086de84f01ba99f73e5c12?d=identicon)[Bioman1464](/maintainers/Bioman1464)

---

Top Contributors

[![MarceauKa](https://avatars.githubusercontent.com/u/1665333?v=4)](https://github.com/MarceauKa "MarceauKa (84 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)")[![mattdfloyd](https://avatars.githubusercontent.com/u/185187?v=4)](https://github.com/mattdfloyd "mattdfloyd (5 commits)")[![ctf0](https://avatars.githubusercontent.com/u/7388088?v=4)](https://github.com/ctf0 "ctf0 (5 commits)")[![Konafets](https://avatars.githubusercontent.com/u/363363?v=4)](https://github.com/Konafets "Konafets (5 commits)")[![Bioman1464](https://avatars.githubusercontent.com/u/36959420?v=4)](https://github.com/Bioman1464 "Bioman1464 (4 commits)")[![riekelt](https://avatars.githubusercontent.com/u/1822200?v=4)](https://github.com/riekelt "riekelt (2 commits)")[![carsso](https://avatars.githubusercontent.com/u/666182?v=4)](https://github.com/carsso "carsso (2 commits)")[![mharacewiat](https://avatars.githubusercontent.com/u/5583430?v=4)](https://github.com/mharacewiat "mharacewiat (2 commits)")[![Livijn](https://avatars.githubusercontent.com/u/349311?v=4)](https://github.com/Livijn "Livijn (1 commits)")[![Lloople](https://avatars.githubusercontent.com/u/5665466?v=4)](https://github.com/Lloople "Lloople (1 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)")[![Vendin](https://avatars.githubusercontent.com/u/9011975?v=4)](https://github.com/Vendin "Vendin (1 commits)")[![bmichotte](https://avatars.githubusercontent.com/u/235510?v=4)](https://github.com/bmichotte "bmichotte (1 commits)")

---

Tags

pluginlaravelpackageauthuserimpersonationimpersonatelaravel-packagelaravel-plugin

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[lab404/laravel-impersonate

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

2.3k15.7M42](/packages/lab404-laravel-impersonate)[rickycezar/laravel-jwt-impersonate

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

25115.0k](/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.

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

PHPackages © 2026

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