laravel-impersonate icon indicating copy to clipboard operation
laravel-impersonate copied to clipboard

impersonate for users in multi-tenancy project with custom domains

Open maherelgamil opened this issue 3 years ago • 2 comments

Hey,

I have multi-tenancy project with custom domains,

So, how can I impersonate users from other domains?

Thanks

maherelgamil avatar Sep 07 '22 17:09 maherelgamil

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.

ju5t avatar May 10 '23 19:05 ju5t