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

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

macromindonline/laravel-impersonate
===================================

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

1.2.1(8y ago)010MITPHPPHP &gt;=7.0

Since Feb 16Pushed 8y ago1 watchersCompare

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

READMEChangelogDependencies (4)Versions (12)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)

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

[](#requirements)

- Laravel &gt;= 5.5
- PHP &gt;= 7

**See release v1.1 for Laravel &lt;= 5.4 support.**

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

[](#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 you must call the `impersonate` route macro.

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

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

// 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_impersonate == 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 isntance.

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
    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)
    Impersonate this user
@endCanBeImpersonated
```

### When the user is impersonated

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

```
@impersonating
    Leave impersonation
@endImpersonating
```

Tests
-----

[](#tests)

```
vendor/bin/phpunit
```

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

[](#contributors)

- [MarceauKa](https://github.com/MarceauKa)
- [tghpow](https://github.com/tghpow)
- and all others [contributors](https://github.com/404labfr/laravel-impersonate/graphs/contributors)

Licence
-------

[](#licence)

MIT

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 57.7% 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 ~20 days

Recently: every ~38 days

Total

11

Last Release

3174d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/ae0f7f1f69309bec448ac224b62d1eb8d0dd6e9477097d4a502cd0f78913042e?d=identicon)[macromindonline](/maintainers/macromindonline)

---

Top Contributors

[![MarceauKa](https://avatars.githubusercontent.com/u/1665333?v=4)](https://github.com/MarceauKa "MarceauKa (41 commits)")[![tghpow](https://avatars.githubusercontent.com/u/6094280?v=4)](https://github.com/tghpow "tghpow (15 commits)")[![mattdfloyd](https://avatars.githubusercontent.com/u/185187?v=4)](https://github.com/mattdfloyd "mattdfloyd (5 commits)")[![Konafets](https://avatars.githubusercontent.com/u/363363?v=4)](https://github.com/Konafets "Konafets (5 commits)")[![svenluijten](https://avatars.githubusercontent.com/u/11269635?v=4)](https://github.com/svenluijten "svenluijten (1 commits)")[![Lloople](https://avatars.githubusercontent.com/u/5665466?v=4)](https://github.com/Lloople "Lloople (1 commits)")[![elbakly](https://avatars.githubusercontent.com/u/2504606?v=4)](https://github.com/elbakly "elbakly (1 commits)")[![hcaz](https://avatars.githubusercontent.com/u/2422207?v=4)](https://github.com/hcaz "hcaz (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/macromindonline-laravel-impersonate/health.svg)

```
[![Health](https://phpackages.com/badges/macromindonline-laravel-impersonate/health.svg)](https://phpackages.com/packages/macromindonline-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)
