Hello,
I am building a website that has several agencies, each of them, related to one domain.
I need to get the agency data in the assigns, similar to what we do with :current_user
.
The user information is stored in the session, but I can’t do that. It won’t make sense store the agency information in the session.
What I have done:
- I have added
plug :fetch_current_agency
to the browser pipeline. For testing purposes the function does nothing yet, but return an arbitrary string:
def fetch_current_agency(conn, _opts) do
# Here I should return and %Agency{} but for simplicity and testing purposes
# I return the host only
assign(conn, :agency, conn.host)
end
- On the
on_mount
callback, I call:ensure_agency
. This is the function:
def on_mount(:ensure_agency, _params, session, socket) do
# Note the agency fallback is only for testing purposes and to make the code simpler
socket = Phoenix.Component.assign_new(socket, :agency, fn -> "Agency fallback" end)
IO.puts("Socket agecny: #{socket.assigns.agency}")
{:cont, socket}
end
On the on_mount
callback, I get the agency right in the first mount call, which is the host. But on the second time, I get the fallback value Agency fallback
My question is, how can I persist the agency data in the assigns?
In this simplified code, I’d like to have the host’s value on the assigns every time so I can access it later in the live views.
Thanks!