Ash FrameworkAF
Ash Framework7mo ago
11 replies
Rutgerdj

How to get tenant in LiveView after setting it using `PlugHelpers.set_tenant`

I'm currently setting my tenant in my controller like this:
conn
|> put_session(:tenant, tenant_id)


And this is how I put it in my LiveView socket assigns:
socket
|> Phoenix.Component.assign(current_tenant: session["tenant"])


I am however getting warnings in my logs saying I should use set_tenant:
[warning] Storing the tenant in conn assigns is deprecated.
  deps/ash/lib/ash/plug_helpers.ex:169: Ash.PlugHelpers.get_tenant/1



So I updated how I set the tenant:
conn
|> Ash.PlugHelpers.set_tenant(tenant_id)

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
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

    tenant =
      if user.is_super_admin? do
        [tenant | _] = Cvs.Tenant.Tenant.read!(actor: user)
        tenant.id
      else
        user.primary_registration.tenant_id
      end

      conn
      |> Ash.PlugHelpers.set_tenant(tenant)
Was this page helpful?