PHP code example of elrod / laravel-multitenancy-impersonate

1. Go to this page and download the library: Download elrod/laravel-multitenancy-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/ */

    

elrod / laravel-multitenancy-impersonate example snippets

bash
php artisan vendor:publish
 php

use elrod\MultitenancyImpersonate\Traits\CanImpersonate;

class ImpersonateController
{
    use CanImpersonate;

    public function redirectTenant($id)
    {
        $tenant = Tenant::find($id);
        
        $redirect_url = "http://{$tenant->domain}/admin";

        $token = $this->createToken($tenant,auth()->user(),$redirect_url);

        $this->impersonate($tenant,$token->token,auth()->user());
            
        $tenant_url = "http://{$tenant->domain}/admin/impersonate";

        return redirect("{$tenant_url}/{$token->token}");
    }

}
 php
Route::get('/admin/impersonate/{token}', function ($token) {

    $impersonate = ImpersonateToken::where('token',$token)->first();

    $user = User::find($impersonate->user_id);

    Auth::login($user);

    return redirect()->route('admin');

});

Route::middleware(['auth:sanctum', 'verified'])->get('/admin', function () {
    return 'Hello World';
})->name('admin');