Phauxth v2.0.0-alpha.5 Phauxth.Authenticate.Base behaviour View Source

Base module for authentication.

This is use-d by Phauxth.Authenticate and Phauxth.Remember, and it is also 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 Phoenix token (using Phauxth.PhxToken) 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 = Phauxth.PhxToken.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 Phauxth.PhxToken.verify, in the user_socket.ex file, to verify the token.

Link to this section Summary

Callbacks

Gets the user based on the session or token data

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

Sets the current_user variable

Link to this section Types

Link to this type error_message() View Source
error_message() :: {:error, String.t()}

Link to this section Callbacks

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

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(arg0, keyword) View Source
report(map() | error_message() | nil, keyword()) :: map() | nil

Logs the result of the authentication and return 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.