How to get tenant in LiveView after setting it using `PlugHelpers.set_tenant`
I'm currently setting my tenant in my controller like this:
And this is how I put it in my LiveView socket assigns:
I am however getting warnings in my logs saying I should use
set_tenant
:
So I updated how I set the tenant:
This then sets the tenant in the private fields as per the docs.
But how do i get that tenant_id in my liveview? Its not stored in the session and I dont have access to the connection.Solution:Jump to solution
ok I think we are going in the right direction, so the authcontroller code is called when you sign in, but then it redirects and there you get a new conn and the browser pipeline only loads the user from the session. You need another plug/controller that you put after load_from_session, where you put this code
```elixir
tenant =
if user.is_super_admin? do...
10 Replies
Thanks for the insights! But I'm still stuck on what the issue could be
I use ash_authentication_phoenix and in my router I do use the
ash_authentication_live_session
:
Anything that could be wrong here? The authcontroller is what is calling the set_tenant
🤔 can you double check that your authcontroller is in the pipeline before it hits the live session?
Yes, I've added some logging to the auth controller and the
live_session
s generate_session
:
As you can see the auth_controller logs that the tenant is being set, but then in the generate_session
the get_tenant
returns nil
is there anything between those in the pipeline that could muck with the conn? or are you maybe missing an assignment or something so you are returning the unmodified conn?
As far as I can tell Im returning the conn with the tenant set. Here is the full AuthController:
And this is the browser pipeline:
Solution
ok I think we are going in the right direction, so the authcontroller code is called when you sign in, but then it redirects and there you get a new conn and the browser pipeline only loads the user from the session. You need another plug/controller that you put after load_from_session, where you put this code
Ah i see, but then that would still require me to set the tenant in the session (so that another plug can get it from the session and set it using
set_tenant
right? I need to somehow communicate to the plug which tenant to set (for example when switching tenants)yeah, you could also just store the tenant in the session in your auth controller and get it from there in a plug instead of repeating that logic. Another approach I have seen is using a subdomain per tenant and get the tenant that way
I understand. I will figure it out from here.
Thanks again for the help!! Really appreciate it! 🧡