Plug.Session options as module attribute for Phoenix.LiveView!?

Thanks again @gcauchon, your solution works like a charm, awesome!
I decided to load session options into the application environment.
Also an inline session module will do the trick.

# MyApp.Application
...

def start(_type, _args) do
  Application.put_env(:my_app, :session_options, [
    store: :cookie,
    key: "session",
    signing_salt: System.fetch_env!("SESSION_SIGNING_SALT")
  ])
  children = [
  ...
end

...

Configure session at runtime.

# MyApp.Endpoint
...

defmodule Session do
  def options do
    Application.get_env(:my_app, :session_options)
  end
end

defp session(conn, _opts) do
  Plug.Session.call(conn, Plug.Session.init(
    Session.options()
  ))
end

...

socket "/live", Phoenix.LiveView.Socket, websocket: [
  connect_info: [session: {Session, :options, []}]
]

...

plug Plug.MethodOverride
plug Plug.Head
plug(:session)
plug MyApp.Router

...
3 Likes