Phauxth v2.3.2 Phauxth.Authenticate.Token behaviour View Source

Base module for token authentication.

This is use-d by Phauxth.AuthenticateToken, and it can also be used to produce a custom token authentication module, as outlined below.

Custom token authentication modules

The next sections give examples of extending this module to create custom authentication modules.

This module will retrieve the token from a cookie, instead of from the headers:

defmodule Phauxth.AuthenticateTokenCookie do
  use Phauxth.Authenticate.Token

  @impl true
  def authenticate(%Plug.Conn{req_cookies: %{"access_token" => token}}, user_context, opts) do
    verify_token(token, user_context, opts)
  end
end

And in the router.ex file, call this plug in the pipeline you want to authenticate.

pipeline :api do
  plug :accepts, ["json"]
  plug AuthenticateTokenCookie
end

GraphQL authentication

The following module is an example of how Phauxth.Authenticate.Token module can be extended, this time to provide authentication for absinthe-elixir:

defmodule AbsintheAuthenticate do
  use Phauxth.Authenticate.Token

  @impl true
  def set_user(user, conn) do
    Absinthe.Plug.put_options(conn, context: %{current_user: user})
  end
end

As in the above example, in the router.ex file, call this plug in the pipeline you want to authenticate.

Link to this section Summary

Callbacks

Gets the token from the authorization headers.

Link to this section Callbacks

Link to this callback

get_token(list, module, keyword) View Source
get_token(list(), module(), keyword()) ::
  {:ok, map()} | {:error, String.t() | atom()}

Gets the token from the authorization headers.