Packages
phauxth
2.5.1
2.5.1
retired
2.5.0
2.4.0
2.3.2
2.3.1
2.3.0
2.2.0
2.1.1
2.1.0
2.0.1
2.0.0
2.0.0-rc.2
2.0.0-rc.1
2.0.0-rc.0
2.0.0-beta.1
2.0.0-beta.0
2.0.0-alpha.5
2.0.0-alpha.4
retired
2.0.0-alpha.3
retired
2.0.0-alpha.2
retired
2.0.0-alpha.1
retired
2.0.0-alpha.0
retired
1.2.9
1.2.8
1.2.7
1.2.6
1.2.5
1.2.4
1.2.3
1.2.1
1.2.0
1.1.3
1.1.2
1.1.1
1.1.0
1.0.3
1.0.2
1.0.1
1.0.0
0.17.1
0.17.0
0.16.0
0.15.1
0.15.0
0.14.0
0.13.0-rc.1
0.13.0-rc.0
0.12.1-rc.1
0.12.1-rc.0
0.12.0-rc
0.11.0
0.10.3
0.10.2
0.10.0
0.9.0
0.8.2
0.8.1
0.8.0
Authentication library for Phoenix, and other Plug-based, web applications
Retired package: Deprecated - Package no longer maintained
Current section
Files
Jump to
Current section
Files
lib/phauxth/authenticate/remember.ex
defmodule Phauxth.Authenticate.Remember do
@moduledoc """
Base module for remember me functionality.
This is `use`-d by Phauxth.Remember, and it can also be used
to produce a custom remember me module.
"""
defmacro __using__(_) do
quote do
use Phauxth.Authenticate.Base
alias Phauxth.Config
@max_age 7 * 24 * 60 * 60
@impl Plug
def init(opts) do
create_session_func =
opts[:create_session_func] ||
raise """
Phauxth.Remember - you need to set a `create_session_func` in the opts
"""
unless is_function(create_session_func, 1) do
raise """
Phauxth.Remember - the `create_session_func` should be a function
that takes one argument
"""
end
opts =
opts
|> Keyword.put_new(:max_age, 14_400)
|> Keyword.put_new(:token_module, Config.token_module())
|> super()
{create_session_func, opts}
end
@impl Plug
def call(%Plug.Conn{assigns: %{current_user: %{}}} = conn, _), do: conn
def call(%Plug.Conn{req_cookies: %{"remember_me" => _}} = conn, {create_session_func, opts}) do
conn |> super(opts) |> add_session(create_session_func)
end
def call(conn, _), do: conn
@impl Phauxth.Authenticate.Base
def authenticate(%Plug.Conn{req_cookies: %{"remember_me" => token}}, user_context, opts) do
token_module = opts[:token_module]
with {:ok, user_id} <- token_module.verify(token, opts),
do: get_user({:ok, %{"user_id" => user_id}}, user_context)
end
@impl Phauxth.Authenticate.Base
def set_user(nil, conn), do: super(nil, delete_rem_cookie(conn))
def set_user(user, conn), do: super(user, conn)
defp add_session(%Plug.Conn{assigns: %{current_user: %{}}} = conn, create_session_func) do
{:ok, %{id: session_id}} = create_session_func.(conn)
conn
|> put_session(:phauxth_session_id, session_id)
|> configure_session(renew: true)
end
defp add_session(conn, _), do: conn
defoverridable Phauxth.Authenticate.Base
end
end
end