Phauxth v0.8.1 Phauxth.Authenticate.Base

Base module for authentication.

This is used by Phauxth.Authenticate and Phauxth.Remember. It can also be used to produce a custom authentication module, as outlined below.

Custom authentication modules

One example of a custom authentication module is provided by the Phauxth.Remember module, which uses this base module to provide the ‘remember me’ functionality.

Graphql authentication

The following module is a more complicated example of how this Base module can be extended, this time to provide authentication for absinthe-elixir:

defmodule AbsintheAuthenticate do

  use Phauxth.Authenticate.Base
  import Plug.Conn
  alias Phauxth.Config

  def call(%Plug.Conn{req_headers: headers} = conn, {context, max_age}) do
    check_headers(headers, context, max_age) |> set_absinthe_user(conn)
  end
  def call(conn, _), do: conn

  defp set_absinthe_user(nil, conn), do: conn
  defp set_absinthe_user({:error, _}, conn), do: conn
  defp set_absinthe_user(user, conn) do
    user = Map.drop(user, Config.drop_user_keys)
    put_private(conn, :absinthe, %{context: %{current_user: user}})
  end
end

Summary

Functions

Check the headers for an authorization token

Check the conn to see if the user is registered in the current session

Check the authorization token

Set the current_user value

Functions

check_headers(headers, context, max_age)

Check the headers for an authorization token.

This function also calls the database to get user information.

check_session(conn)

Check the conn to see if the user is registered in the current session.

This function also calls the database to get user information.

check_token(token, context, max_age)

Check the authorization token.

This function also calls the database to get user information.

set_current_user(user, conn)

Set the current_user value.