Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon 404labfr/laravel-impersonate
    Laravel Service Provider


404labfr/laravel-impersonate


The package `404labfr/laravel-impersonate` allows you to impersonate users in your Laravel application. Here are the key points about this package:

Installation and Setup

1. Installation: You can install the package using Composer:
bash
    composer require 404labfr/laravel-impersonate
    

2. Service Provider: Add the service provider to your `config/app.php` file:
php
    'providers' => [
        // ...,
        Lab404\Impersonate\ImpersonateServiceProvider::class,
    ],
    

3. User Model: Add the `Impersonate` trait to your `User` model:
php
    namespace App\Models;

    use Illuminate\Foundation\Auth\User as Authenticatable;
    use Lab404\Impersonate\Models\Impersonate;

    class User extends Authenticatable
    {
        use Impersonate;
    }
    

Usage

1. Impersonate a User:
php
    Auth::user()->impersonate($otherUser);
    

This will log you in as the specified user.

2. Leave Impersonation:

php
    Auth::user()->leaveImpersonation();
    

This will log you back in as your original user.

Configuration

1. Blade Directives: You can use blade directives to check if a user is impersonating or not:
php
    @if(Auth::user()->isImpersonating())
        // User is impersonating
    @endif
    

Issues and Support

1. GitHub Issues: The package has an active issue tracker on GitHub, where users can report bugs and request features.
2. Stack Overflow: There are also questions and answers on Stack Overflow related to using this package with Auth0 and other Laravel versions.

Compatibility

1. Laravel Versions: The package supports Laravel versions 6.x to 11.x.
2. PHP Versions: It supports PHP versions 7.2 and above.

Example Tutorial

1. Laravel 9 User Impersonation Example: A step-by-step tutorial on how to implement user impersonation in Laravel 9 using the `404labfr/laravel-impersonate` package[4].

Additional Features

1. Restricting Impersonation: You can restrict who can impersonate users and protect specific routes from being accessed by impersonators.
2. Blade Macros: The package includes blade macros to easily check if a user is impersonating or not.

Overall, `404labfr/laravel-impersonate` is a robust and widely used package for user impersonation in Laravel applications, providing a simple and secure way to manage user access and permissions[1][2][5].

Citations:
[1] https://www.youtube.com/watch?v=E6qgAizaMEw
[2] https://github.com/404labfr/laravel-impersonate/issues
[3] https://stackoverflow.com/questions/77888806/laravel-10-auth0-and-404labfr-laravel-impersonate
[4] https://codeanddeploy.com/blog/laravel/laravel-8-user-impersonation-example
[5] https://github.com/404labfr/laravel-impersonate