Search code examples
laravelmulti-tenanttenancyforlaravel

How to achieve User impersonation through TenancyforLaravel?


I want my admin user to impersonate my standard user. I'm using TenancyforLaravel package (https://tenancyforlaravel.com/docs/v3/features/user-impersonation/) with single database multi-subdomain installation.

My admin user want to ghost login to my tenant dashboard console and after that I want to logout as tenant user and switch back to my admin role profile.

I'm not able to achieve the impersonation. Can someone please guide me.


Solution

  • For ghost login, below is the code. Here "/home" can be replaced with your route where you want your user to be landed after impersonation but relative value only.

    $user = User::find($id);
    $tenant = $user->tenant;
    $token = tenancy()->impersonate($tenant, $id, '/home');
    $domain = $user->tenant_domain->domain;
    $url = "http://$domain/impersonate/{$token->token}";
    return redirect()->away($url);
    

    This will give us the temporary login URL to achieve impersonation.