Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Tenant Finder using authenticated user #53

Closed
henryavila opened this issue Jun 19, 2020 · 7 comments
Closed

Custom Tenant Finder using authenticated user #53

henryavila opened this issue Jun 19, 2020 · 7 comments

Comments

@henryavila
Copy link

Hi.
In my case, The Tenant is linked to User and not to domain (All tenants will access the app from the same domain).
But when I run Auth::user() in TenantFinder@findForRequest I always get null.

How can I access The authenticated user inside TenantFinder?

@masterix21
Copy link
Collaborator

Skip the finder and add two listeners for these events:

  • Illuminate\Auth\Events\Login
  • Illuminate\Auth\Events\Logout

When the login event will be fired, do your $tenant->makeCurrent(), and when the logout event will be fired, do your $tenant->forgot().

I think it’s better for you, try and give me feedback.

@sandulat
Copy link

sandulat commented Jun 24, 2020

@masterix21 @freekmurze If I do the $tenant->makeCurrent() in the login event listener, the tenant is being set, however, after I'm redirected, it is not being persisted and it becomes null. Is there any step that I'm missing? Or is this something on my side?

Basically I'm just trying to switch the tenant on the session level, not domain level.

I'm not sure if this package persists the tenant in the session. If not, I've tried to manually store the tenant id in the session, when the MadeTenantCurrentEvent is fired. However, the session is not being set in the TenantFinder and I can't read the id.

@masterix21
Copy link
Collaborator

If you call request()->auth() before the StartSession middleware, you'll get null always. I can suggest using another approach: instead of work with a Finder, write a middleware (in web, after StartSession) to find your tenant. It will work.

@masterix21
Copy link
Collaborator

Last but not least, add a listener to Illuminate\Auth\Events\Authenticated event... it's another solution ;)

@henryavila
Copy link
Author

Just to register, the middleware approach gave me the needed flexibility. Worked perfectly.
I set the tenant before the return $next($request);

@dambridge
Copy link
Sponsor

Sorry for the necro bump, just curious to see what you ended up doing. I too want to abandon the whole subdomain thing and ultimately be able to attach users to multiple tenants.

Question: if we just check for a tenant id in middleware, are we not losing some part of the identification pipeline this package needs?

@masterix21
Copy link
Collaborator

masterix21 commented Aug 6, 2020

@dambridge, the package works if you set a tenant "as current": it does not care if you do it from a middleware or a task.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants