You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
analytics/lib/plausible_web/plugs/runtime_session_adapter.ex

31 lines
747 B
Elixir

defmodule PlausibleWeb.Plugs.RuntimeSessionAdapter do
@moduledoc """
A `Plug.Session` adapter that allows configuration at runtime.
Sadly, the plug being wrapped has no MFA option for dynamic
configuration.
This is currently used so we can dynamically pass the :domain
and have cookies planted across one root domain.
"""
@behaviour Plug
@impl true
def init(opts) do
Plug.Session.init(opts)
end
@impl true
def call(conn, opts) do
Plug.Session.call(conn, patch_cookie_domain(opts))
end
defp patch_cookie_domain(%{cookie_opts: cookie_opts} = runtime_opts) do
Map.replace(
runtime_opts,
:cookie_opts,
Keyword.put_new(cookie_opts, :domain, PlausibleWeb.Endpoint.host())
)
end
end