Phauxth v2.0.0-rc.1 Phauxth.Authenticate.Base behaviour View Source

Base module for authentication.

This is use-d by Phauxth.Authenticate and Phauxth.Remember, and it is extended by Phauxth.Authenticate.Token.

This module can also be used to produce a custom authentication module, as outlined below.

Custom authentication modules

The next section gives examples of extending this module to create custom authentication modules.

Examples

Authentication for use with Phoenix channels

In this example, after adding the user struct to the current_user value, a token is added to the conn.

defmodule MyAppWeb.ChannelAuthenticate do
  use Phauxth.Authenticate.Base

  def set_user(nil, conn), do: assign(conn, :current_user, nil)

  def set_user(user, conn) do
    token = MyAppWeb.Token.sign(%{"user_id" => user.email})
    user |> super(conn) |> assign(:user_token, token)
  end
end

MyAppWeb.ChannelAuthenticate is called in the same way as Phauxth.Authenticate.

You can then use MyAppWeb.Token.verify, in the user_socket.ex file, to verify the token - see the documentation for Phauxth.Token for information about how to create the MyAppWeb.Token module.

Link to this section Summary

Callbacks

Gets the user based on the session or token data

Logs the result of the authentication and returns the user struct or nil

Sets the current_user variable

Link to this section Types

Link to this type ok_or_error() View Source
ok_or_error() :: {:ok, map()} | {:error, String.t() | atom()}

Link to this section Callbacks

Link to this callback authenticate(arg0, module, keyword) View Source
authenticate(Plug.Conn.t(), module(), keyword()) :: ok_or_error()

Gets the user based on the session or token data.

In the default implementation, this function also retrieves user information using the get_by function defined in the user_context module.

Link to this callback report(ok_or_error, keyword) View Source
report(ok_or_error(), keyword()) :: map() | nil

Logs the result of the authentication and returns the user struct or nil.

Link to this callback set_user(arg0, arg1) View Source
set_user(map() | nil, Plug.Conn.t()) :: Plug.Conn.t()

Sets the current_user variable.