archtechx / tenancy

Automatic multi-tenancy for Laravel. No code changes needed.
https://tenancyforlaravel.com
MIT License
3.67k stars 432 forks source link

Call to a member function getTenantKey() on null #1227

Closed jainsiddharth21 closed 6 months ago

jainsiddharth21 commented 6 months ago

Bug description

Getting error Call to a member function getTenantKey() on null at line vendor/stancl/tenancy/src/Features/UserImpersonation.php:42 during user Impersonation.

Steps to reproduce

  1. Finding the user using ID
  2. Getting the tenant & domain information using relationships
  3. Generating the impersonation link using domain and token
  4. When opening the URL, getting the error Call to a member function getTenantKey() on null at line vendor/stancl/tenancy/src/Features/UserImpersonation.php:42
$user = User::find($id);
$tenant = $user->tenant;
$token = tenancy()->impersonate($tenant, $id, '/home');
$domain = $user->tenantdomain->domain;
return redirect("http://$domain/impersonate/{$token->token}");

Expected behavior

It should allow the admin user to be impersonated and logged into the system

Laravel version

8.83.27

stancl/tenancy version

3.6.1

stancl commented 6 months ago

There needs to be a current tenant:

https://github.com/archtechx/tenancy/blob/8f9c7efa4584007f41048448d0a11f572a1d3239/src/Features/UserImpersonation.php#L42

https://tenancyforlaravel.com/docs/v3/features/user-impersonation/#usage

jainsiddharth21 commented 6 months ago

@stancl Thanks for the prompt response. But, the implementation is not clear to me from this documentation https://tenancyforlaravel.com/docs/v3/features/user-impersonation/#usage. I've implemented the multi-tenancy using this documentation only.

I'm on the central domain (mydomain.com) and wants to impersonate to tenant (tenant1.mydomain.com).

When I dd(tenant()) on the /dashboard it results me null value. Is there anything wrong I'm doing ?

$user = User::find($id);
$tenant = $user->tenant; // relationship
$token = tenancy()->impersonate($tenant, $id, '/dashboard');
$domain = $user->tenantdomain->domain; // tenant1.mydomain.com
return redirect("http://$domain/impersonate/{$token->token}");

If possible could you please share the steps what I'm doing wrong to correct this or if we can connect over call that would be greatly appreciated.

Also, this redirection is also not happening. Even if I put the static https://www.google.com url or the laravel route. Can you guide me on that as well. ![Uploading Screenshot 2024-05-24 at 5.24.26 PM.png…]()