diff --git a/lib/web/plug/session.ex b/lib/web/plug/session.ex index c73a823b..aa731a91 100644 --- a/lib/web/plug/session.ex +++ b/lib/web/plug/session.ex @@ -20,9 +20,14 @@ defmodule Antikythera.Plug.Session do alias Antikythera.Conn alias Antikythera.Session + alias Antikythera.Http - defun load(conn :: v[Conn.t()], opts :: Keyword.t(String.t() | atom)) :: Conn.t() do + @type load_option_t :: + {:key, String.t()} | {:store, atom} | {:set_cookie, Http.SetCookie.options_t()} + + defun load(conn :: v[Conn.t()], opts :: v[list(load_option_t)]) :: Conn.t() do key = opts[:key] + set_cookie_opts = Keyword.get(opts, :set_cookie, %{}) store_name = Keyword.get(opts, :store, :cookie) |> Atom.to_string() |> Macro.camelize() store_module = Module.safe_concat("Antikythera.Session", store_name) {session_id, data} = store_module.load(Conn.get_req_cookie(conn, key)) @@ -34,23 +39,24 @@ defmodule Antikythera.Plug.Session do } conn - |> Conn.register_before_send(make_before_send(store_module, key)) + |> Conn.register_before_send(make_before_send(store_module, key, set_cookie_opts)) |> Conn.assign(:session, session) end - defunp make_before_send(store :: module, key :: String.t()) :: (Conn.t() -> Conn.t()) do + defunp make_before_send(store :: module, key :: String.t(), set_cookie_opts :: map) :: + (Conn.t() -> Conn.t()) do fn %Conn{assigns: %{session: session}} = conn -> %Session{state: state, id: id, data: data} = session case state do :update -> new_id = store.save(id, data) - Conn.put_resp_cookie(conn, key, new_id) + Conn.put_resp_cookie(conn, key, new_id, set_cookie_opts) :renew -> store.delete(id) new_id = store.save(nil, data) - Conn.put_resp_cookie(conn, key, new_id) + Conn.put_resp_cookie(conn, key, new_id, set_cookie_opts) :destroy -> store.delete(id)