Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read Set-Cookie header options from Session plug's options #187

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions lib/web/plug/session.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ defmodule Antikythera.Plug.Session do
alias Antikythera.Conn
alias Antikythera.Session

defun load(conn :: v[Conn.t()], opts :: Keyword.t(String.t() | atom)) :: Conn.t() do
key = opts[:key]
defun load(conn :: v[Conn.t()], opts :: Keyword.t(String.t() | atom | map)) :: Conn.t() do
irisTa56 marked this conversation as resolved.
Show resolved Hide resolved
key = opts[:key]
aYukiSekiguchi marked this conversation as resolved.
Show resolved Hide resolved
set_cookie_opts = opts[:set_cookie]
irisTa56 marked this conversation as resolved.
Show resolved Hide resolved
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))
Expand All @@ -34,23 +35,23 @@ 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)
Expand Down