James Ball

Web Developer

Laravel Impersonate

I’ve recently developed a Laravel Package to temporarily login as other users when signed in as an admin, Download here.

Usage

  • You can login to another user through {app_url}/impersonate/login/{user_id}.
  • And you can end the session with {app_url}/impersonate/logout.

Install Package

1
composer require j84115/impersonate

Add Sevice Provider

Add the Package to config/app.php

1
J84115\Impersonate\ImpersonateServiceProvider::class,

Add Interface To User

Add the Interface to your User Model. Typically app/Models/User.php.

1
use J84115\Impersonate\Interfaces\ImpersonateUser;

Implement the interface.

1
class User extends Authenticatable implements ImpersonateUser

Then add your conditions for who can impersonate a user.

1
2
3
4
5
6
7
8
9
public function impersonator(): bool
{
    return $this->role === 'admin';
}

public function impersonatable(): bool
{
    return $this->email !== 'admin';
}

Routing

Add the following macro to your routes. Typically guarded with auth Middleware in routes/web.php.

1
Route::impersonate();