Dynamically set up session domain (#3089)
* Dynamically configure session domain * Fix up error message in runtime configpull/3090/head r2023.06.28-136
parent
4ac83b6e5d
commit
eb397a6c25
@ -0,0 +1,30 @@
|
||||
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
|
||||
Loading…
Reference in New Issue