PHP code example of rickycezar / laravel-jwt-impersonate

1. Go to this page and download the library: Download rickycezar/laravel-jwt-impersonate library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rickycezar / laravel-jwt-impersonate example snippets


'providers' => [
    // ...
    Rickycezar\Impersonate\ImpersonateServiceProvider::class,
],

$token = Auth::user()->impersonate($other_user);
// You're now logged as the $other_user and the authentication token is stored in $token.

$token = Auth::user()->leaveImpersonation();
// You're now logged as your original user and the authentication token is stored in $token.

Route::impersonate();

namespace App\Providers;

class RouteServiceProvider extends ServiceProvider
{
    public function map() {
        Route::middleware('web')->group(function (Router $router) {
            $router->impersonate();
        });
    }
}

// Where $id is the ID of the user you want impersonate
route('impersonate', $id) //the url path is "impersonate/take/{id}".

// Generate an URL to leave current impersonation
route('impersonate.leave') //the url path is "impersonate/leave".

// Check the current user impersonation status
route('impersonate.info') //the url path is "impersonate/info".

    /**
     * @return bool
     */
    public function canImpersonate()
    {
        // For example
        return $this->is_admin == 1;
    }

    /**
     * @return bool
     */
    public function canBeImpersonated()
    {
        // For example
        return $this->can_be_impersonated == 1;
    }

use Rickycezar\Impersonate\Services\ImpersonateManager;

class ImpersonateController extends Controller
{
    protected $manager;
    
    // Dependency Injection
    public function __construct(ImpersonateManager $manager)
    {
        $this->manager = $manager;
    }

    public function impersonate(){ /*....*/ }
    public function leave(){ /*....*/ }
}

class ImpersonateController extends Controller
{
    protected $manager;
        
    //Direct app call
    public function __construct()
    {
        $this->manager = app('impersonate');
    }

    public function impersonate(){ /*....*/ }
    public function leave(){ /*....*/ }
}

$manager = app('impersonate');

// Find a user by its ID
$manager->findUserById($id);

// TRUE if you are impersonating an user.
$manager->isImpersonating();

// Impersonate a user. Pass the original user and the user you want to impersonate. Returns authentication token
$token = $manager->take($from, $to);

// Leave current impersonation. Returns authentication token
$token = $manager->leave();

// Get the impersonator ID
$manager->getImpersonatorId();

Router::get('/my-credit-card', function() {
    echo "Can't be accessed by an impersonator";
})->middleware('impersonate.protect');

    // The custom claim key used to store the original user id in the JWT token.
    'session_key' => 'impersonated_by',

    // The alias for the authentication middleware to be used in the routes.
    'auth_alias' => 'auth',
bash
php artisan vendor:publish --tag=impersonate