PHPackages                             hashmatwaziri/laravel-multi-auth-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. hashmatwaziri/laravel-multi-auth-impersonate

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

hashmatwaziri/laravel-multi-auth-impersonate
============================================

laravel-multi-auth-impersonate

v1.0(5y ago)335↓94.4%MITPHPPHP ^7.2 | ^8.0

Since Jan 20Pushed 3y ago1 watchersCompare

[ Source](https://github.com/HashmatWaziri/laravel-multi-auth-impersonate)[ Packagist](https://packagist.org/packages/hashmatwaziri/laravel-multi-auth-impersonate)[ Docs](https://github.com/HashmatWaziri/laravel-multi-auth-impersonate)[ GitHub Sponsors](https://github.com/sponsors/HashmatWaziri)[ Fund](https://hashmatwaziri.com)[ RSS](/packages/hashmatwaziri-laravel-multi-auth-impersonate/feed)WikiDiscussions laravel-10 Synced 2d ago

READMEChangelog (1)Dependencies (5)Versions (4)Used By (0)

laravel-multi-auth-impersonate
==============================

[](#laravel-multi-auth-impersonate)

[![Latest Version on Packagist](https://camo.githubusercontent.com/f26c4202e7db4dd3fe4b25107b0457033d0823a48c4c0c4c4f37118684cf4c0c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f486173686d617457617a6972692f6c61726176656c2d6d756c74692d617574682d696d706572736f6e6174652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/HashmatWaziri/laravel-multi-auth-impersonate)

[![Total Downloads](https://camo.githubusercontent.com/cdd7b38998e4bfd9b7451fa03855de7a937f747442960f6d20bfa98972690e39/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f486173686d617457617a6972692f6c61726176656c2d6d756c74692d617574682d696d706572736f6e6174652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/HashmatWaziri/laravel-multi-auth-impersonate)

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

[](#requirements)

- Php &gt; 7.3 or 8

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

[](#installation)

You can install the package via composer:

```
composer require HashmatWaziri/laravel-multi-auth-impersonate
```

You can publish the config file with:

```
php artisan vendor:publish --provider="HashmatWaziri\LaravelMultiAuthImpersonate\LaravelMultiAuthImpersonateServiceProvider" --tag="multiAuthImpersonate"
```

This is the contents of the published config file:

```
return [
 /**
     * The session key used to store the original user id.
     */
    'session_key' => 'impersonated_by',

    /**
     * The session key used to stored the original user guard.
     */
    'session_guard' => 'impersonator_guard',

    /**
     * The session key used to stored what guard is impersonator using.
     */
    'session_guard_using' => 'impersonator_guard_using',

    /**
     * The default impersonator guard used.
     */
    'default_impersonator_guard' => 'web',
];
```

### Redirect URLs

[](#redirect-urls)

**take Redirect** : when impersonating another user, you can add the method `takeRedirectTo()` to your model which is being impersonated:

example:

```
  class User extends Authenticatable implements MustVerifyEmail
{

    use Notifiable,Impersonate;

    public static function takeRedirectTo(){

        return url('/after-being-impersonated');
    }

}
```

**leave Redirect** : when an impersonator ( the one who impersonated or logged in as another user) is leaving the impersonation, you can add the method `leaveRedirectTo()` to that model:

example:

```
  class Employee extends Authenticatable implements MustVerifyEmail
{

    use Notifiable,Impersonate;

    public static function leaveRedirectTo(){

        return url('/workplace/dashboard');
    }

}
```

Usage
-----

[](#usage)

Impersonate a user:

```
$other_user = App\Student::find(1);
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.
```

### Routes

[](#routes)

In your routes file, under web middleware, you must call the `multiAuthImpersonate` route macro with any route name you choose to be used for this package. This package let user decides on which package URL these routes should be registered

```
Route::multiAuthImpersonate('impersonation');
```

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

```
namespace App\Providers;

class RouteServiceProvider extends ServiceProvider
{
    public function map() {
	// here you can supply an array of guards ex ['web','employee','etc'] so that each can impersonate other
        Route::middleware('web')->group(function (Router $router) {
            $router->multiAuthImpersonate('impersonation');
        });
    }
}
```

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

// You should also add `guardName`
route('impersonate', ['id' => $id, 'guardName' => 'admin'])

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

### Defining impersonation authorization

[](#defining-impersonation-authorization)

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

```
  class User extends Authenticatable implements MustVerifyEmail
{

    use Notifiable,Impersonate;

//    /**
//     * @return bool
//     */
    public function canImpersonate()
    {

        return true ;

    }

}
```

By default all users can **be impersonated**.
You need to add the method `canBeImpersonated()` to your guard 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();
```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [HashmatWaziri](https://github.com/HashmatWaziri)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

28

—

LowBetter than 51% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 100% 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

Unknown

Total

1

Last Release

2036d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/10204499?v=4)[Hashmat Waziri](/maintainers/hashmatwaziri)[@HashmatWaziri](https://github.com/HashmatWaziri)

---

Top Contributors

[![HashmatWaziri](https://avatars.githubusercontent.com/u/10204499?v=4)](https://github.com/HashmatWaziri "HashmatWaziri (26 commits)")

---

Tags

laravelauthimpersonationimpersonatelaravel-packagemultiauthlaravel-multi-auth-impersonate

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/hashmatwaziri-laravel-multi-auth-impersonate/health.svg)

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

###  Alternatives

[lab404/laravel-impersonate

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

2.3k19.4M70](/packages/lab404-laravel-impersonate)[rickycezar/laravel-jwt-impersonate

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

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

UI for 404labfr/laravel-impersonate

371.6k](/packages/hapidjus-laravel-impersonate-ui)

PHPackages © 2026

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