Hi I'm using different domain names to load different data sets. I'm currently using a custom plug to load the correct domain id based on the hostname. E.g. got this in my endpoint.ex
just before the router:
plug WebApp.DomainCheck
socket "/live", Phoenix.LiveView.Socket, websocket: [connect_info: [session: @session_options]]
...
plug WebApp.Router
And
defmodule WebApp.DomainCheck do
import Plug.Conn
@behaviour Plug
def init([]), do: []
def call(conn, _options \\ []) do
domains = Model.load_allowed_domains()
case Map.get(domains, conn.host) do
nil ->
conn
|> resp(401, "Domain not allowed")
|> halt()
domain_id ->
conn
|> assign(:domain_id, domain_id)
end
end
end
Now this works for normal View
as I have the domain_id
assign in each of them. But how do I get the domain
data injected into my LiveView
s as well from a plug?
Currently I've code duplicated the same domain check into every LiveViews mount() page:
defmodule WebApp.WelcomeLive do
use WebApp, :live_view
@impl true
def mount(_args, _session, socket) do
domains = Model.load_allowed_domains()
host = socket.host_uri.host
case Map.get(domains, host) do
nil -> {:error, "Domain not allowed"}
domain_id -> {:ok, assign(socket, :domain_id, domain_id)}
end
end
Is there any way I can make a plug effective in pushing this data down to the live views without need to add code to each mount?