authenticator v1.0.0 Authenticator behaviour

Generates a module for authenticating requests based on the Plug.Conn.

Example

defmodule MyAppWeb.Authentication do
  use Authenticator, fallback: MyAppWeb.FallbackController

  alias MyApp.Repo
  alias MyApp.Accounts.User

  @impl true
  def tokenize(user) do
    {:ok, to_string(user.id)}
  end

  @impl true
  def authenticate(user_id) do
    case Repo.get(User, user_id) do
      nil ->
        {:error, :unauthenticated}

      user ->
        {:ok, user}
    end
  end
end

Link to this section Summary

Link to this section Types

Link to this type reason()
reason() :: atom()
Link to this type resource()
resource() :: any()
Link to this type token()
token() :: String.t()

Link to this section Callbacks

Link to this callback authenticate(token)
authenticate(token()) :: {:ok, resource()} | {:error, reason()}
Link to this callback tokenize(resource)
tokenize(resource()) :: {:ok, token()} | {:error, reason()}