404labfr / laravel-impersonate

Laravel Impersonate is a plugin that allows you to authenticate as your users.
https://marceau.casals.fr
2k stars 202 forks source link

impersonate for users in multi-tenancy project with custom domains #168

Open maherelgamil opened 2 years ago

maherelgamil commented 2 years ago

Hey,

I have multi-tenancy project with custom domains,

So, how can I impersonate users from other domains?

Thanks

ju5t commented 1 year ago

We do this too, but we don't have a strict requirement to switch domains when we impersonate a user. For Spatie's multi tenancy solution you can use middleware to set the tenant. This assumes you have a relation set on your User-model.

public function handle(Request $request, Closure $next): mixed
{
    if (app('impersonate')->isImpersonating()) {
        auth()->user()->tenant->makeCurrent();
    }

    return $next($request);
}

We're not using this in production yet but I have not found any issues with it.