This package is abandoned and no longer maintained. No replacement package was suggested.

Impersonate users easily in Laravel apps.

Installs: 2 961

Dependents: 0

Suggesters: 0

Security: 0

Stars: 27

Watchers: 5

Forks: 6

Open Issues: 0


v0.4.0 2020-01-15 10:24 UTC

This package is auto-updated.

Last update: 2020-09-09 16:58:23 UTC


A lightweight, yet useful package for user impersonation.

Impersonation can be a nice way to track down issues / bugs beyond our tests. With this package, it's easy to implement it, without touching the core of our app.

Configure the package as you need and it's ready to go!

Getting Started

You can install the package with composer, using the composer require thepinecode/impersonate command.

Laravel 5.5

If you are using version 5.5, there is nothing else to do. Since the package supports autodiscovery, Laravel will register the service provider automatically, behind the scenes.

Laravel 5.4 and below

You have to register the service provider manually. Go to the config/app.php file and add the Pine\Impersonate\ImpersonateServiceProvider::class to the providers array.

Disable the autodiscovery for the package

In some cases you may disable autodiscovery for this package. You can add the provider class to the dont-discover array to disable it.

Then you need to register it manually again.


You may override the default configurations. To do that, first you have to publish the config file with the following command: php artisan vendor:publish --provider=Pine\Impersonate\ImpersonateServiceProvider. Then you can find the config file, at config/impersonate.php.


By default the impersonating routes have with two middlewares, the web and the auth. But of course, it's definetly not enough. In the config file, at the middlewares section, you can add more middlewares, what you registered in your Kernel.php before, or you can add fully qualified classnames as well.


The package brings two routes. One for imopersonating a user and one for revert to the original one.

The impersonate route's properties are the following:

  • Path: /impersonate/{user}
  • Name: impersonate.impersonate
  • Controller@action: Pine\Impersonate\Http\Controllers\ImpersonateController@impersonate

The revert route's properties are the following:

  • Path: /impersonate/revert
  • Name: impersonate.revert
  • Controller@action: Pine\Impersonate\Http\Controllers\ImpersonateController@revert

As you see, when we navigate a user to these routes, the action has to hit the server. That means, if you use something like Vue Router, please take care to use a normal link for these instead of the router's.

<!-- Bad, it will hit the router -->
<router-link to="/impersonate/revert">Revert</router-link>

<!-- Good, it will hit the server -->
<a href="/impersonate/revert">Revert</a>


After impersonating a user, or reverting we return with a redirection response. By default both action redirects to the /home path. You can configure both routes individually in the config file, at the redirect section.


Both actions trigger an event when we hit them. You can setup your own listeners for these events, in the EventServiceProvider. For example:

protected $listen = [
    \Pine\Impersonate\Events\ChangedToUser::class => [
        // Listeners
    \Pine\Impersonate\Events\Reverted::class => [
        // Listeners

Also, you can access the impersonated user via the ChangedToUser event. Here is an example listener for that:

public function handle(ChangedToUser $event)
    // $event->user

Blade Directive

The package brings a blade directive to check easily if the user is in an impersonation mode or not. It's like a basic if statement.

    // You are impersonating
    // You are in your own profile

Working with JS

Since the whole process is session based, we have the information about the current state only on the server-side. That means, if we want to integrate the package with an SPA, we need to extract the info and make it accessible in the front-end.

At the first load, we have the chance to fetch the current status to the window object. For example:

        window.isImpersonating = true;
        window.isImpersonating = false;


If you found a bug or you have an idea connecting the package, feel free to open an issue.