elixir-plug / plug

Compose web applications with functions
https://hex.pm/packages/plug
Other
2.84k stars 582 forks source link

Add option for calling Plug.Session with MFA for runtime config #1140

Closed jsonmaur closed 1 year ago

jsonmaur commented 1 year ago

This allows the session and cookie options to be dynamically configured during runtime, which is already supported in the websocket options of Phoenix.Endpoint but not supported here. My main use case was setting secure: true in production if using HTTPS behind SSL termination (so it's not automatically set). Example usage:

config :my_app, :session,
  store: :cookie,
  key: "_my_app_session",
  signing_salt: "12345",
  same_site: "Lax"
socket "/live", Phoenix.LiveView.Socket,
  websocket: [
    connect_info: [session: {Application, :fetch_env!, [:my_app, :session]}]
  ]

plug Plug.Session, {Application, :fetch_env!, [:my_app, :session]}
josevalim commented 1 year ago

Hi @jsonmaur. In this case you can call the plug yourself:

plug :app_session

def app_session(conn, _opts) do
  env = Application.fetch_env!(...)
  Plug.Session.call(conn, Plug.Session.init(env))
end

:)