Packages
phauxth
2.0.0-beta.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
Current section
Files
Jump to
Current section
Files
lib/phauxth/confirm/base.ex
defmodule Phauxth.Confirm.Base do
@moduledoc """
Base module for handling user confirmation.
This is used by Phauxth.Confirm and Phauxth.Confirm.PassReset,
and it can also be used to create custom user confirmation modules.
"""
@doc false
defmacro __using__(_) do
quote do
@behaviour Phauxth
alias Phauxth.{Config, Log}
@impl true
def verify(params, opts \\ [])
def verify(%{"key" => token} = params, opts) do
log_meta = Keyword.get(opts, :log_meta, [])
params |> authenticate(opts) |> report(log_meta)
end
def verify(_, _), do: raise(ArgumentError, "No key found in the params")
@impl true
def authenticate(%{"key" => token}, opts) do
token
|> Config.token_module().verify(opts ++ [max_age: 1200])
|> get_user()
end
defp get_user({:ok, data}) do
case Config.user_context().get_by(data) do
nil -> {:error, "no user found"}
user -> {:ok, user}
end
end
defp get_user({:error, message}), do: {:error, message}
@impl true
def report({:ok, user}, meta) do
Log.info(%Log{user: user.id, message: "user confirmed", meta: meta})
{:ok, Map.drop(user, Config.drop_user_keys())}
end
def report({:error, message}, meta) do
Log.warn(%Log{message: message, meta: meta})
{:error, Config.user_messages().default_error()}
end
defoverridable Phauxth
end
end
end