Hello Folks,
How to use Phoenix VefifiedRoutes path_prefixes (Phoenix.VerifiedRoutes — Phoenix v1.8.0-rc.4), when i try something like that on my MyAppWeb like:
def verified_routes do
quote do
use Phoenix.VerifiedRoutes,
endpoint: MyAppWeb.Endpoint,
router: MyAppWeb.Router,
statics: MyAppWeb.static_paths(),
path_prefixes: [{MyApp.Tenant, :get_path, [@tenant]}]
end
end
but the static path lost the host part or add two slashs in prefix and can’t render.
and the @tenant is not understand by the code…
i need some routes like, http://sub.myurl.com/tenant/ ← when the tenant is dynamic
in almost all routes.
To do this, you’ll need to store the tenant in the process dictionary
defmodule MyApp.Tenant do
@tenant_key {__MODULE__, :tenant_id}
def put_tenant_id(tenant_id) do
Process.put(@tenant_key, tenant_id)
end
def get_tenant_id() do
Process.get(@tenant_key)
end
end
def verified_routes do
quote do
use Phoenix.VerifiedRoutes,
endpoint: MyAppWeb.Endpoint,
router: MyAppWeb.Router,
statics: MyAppWeb.static_paths(),
path_prefixes: [{MyApp.Tenant, :get_tenant_id, []}]
end
end
# Some session plug
def get_and_verify_tenant() do
#tenant = get_tenant…
MyApp.Tenant.put_tenant_id(tenant.id)
#…
end
It can be tricky to work with if you also have “non-tenant” routes.
To deal with that, I still had a tenant_id scope
scope "/:tenant_id" do
get "/user/log-in"
end
And then did Process.get(@tenant_key, "_") so I ended up with URLs like /_/user/log-in