Phauxth.Authenticate.Base behaviour (Phauxth v2.5.1) 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 Callbacks

Link to this callback

authenticate(arg1, module, keyword)

View Source

Specs

authenticate(Plug.Conn.t(), module(), keyword()) ::
  {:ok, map()} | {:error, String.t() | atom()}

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.

Specs

report(
  {:ok, map()} | {:error, String.t() | atom()},
  keyword()
) :: map() | nil

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

Specs

set_user(map() | nil, Plug.Conn.t()) :: Plug.Conn.t()

Sets the current_user variable.