Phauxth v2.0.0-alpha.4 Phauxth.Confirm.Base behaviour View Source

Base module for handling user confirmation.

This is used by Phauxth.Confirm and Phauxth.Confirm.PassReset, and it can also be used to create custom user confirmation modules.

Link to this section Summary

Callbacks

Gets the user data using the key in the params

Prints out a log message and then returns {:ok, user} or {:error, message} to the calling function

Verifies the confirmation key and gets the user data from the database

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(binary, map) View Source
get_user(binary(), map()) :: map() | nil

Gets the user data using the key in the params.

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

Link to this callback report(map, keyword) View Source
report(map(), keyword()) :: {:ok, map()} | error_message()

Prints out a log message and then returns {:ok, user} or {:error, message} to the calling function.

Link to this callback verify(map, keyword) View Source
verify(map(), keyword()) :: {:ok, map()} | error_message()

Verifies the confirmation key and gets the user data from the database.

Phauxth.Confirm.verify is used to confirm an email for new users and Phauxth.Confirm.PassReset.verify is used for password resetting.

Options

There are two options for the verify function:

  • :user_context - the users module to be used when querying the database

    • the default is Phauxth.Config.user_context()
  • :log_meta - additional custom metadata for Phauxth.Log

    • this should be a keyword list

In addition, there are also options for verifying the token, including a max_age option, which defaults to 1200 seconds (20 minutes).

Examples

The following function is an example of using Phauxth.Confirm.verify in a Phoenix controller.

def index(conn, params) do
  case Phauxth.Confirm.verify(params) do
    {:ok, user} ->
      Users.confirm_user(user)
      message = "Your account has been confirmed"
      Users.Message.confirm_success(user.email)
      handle_success() # redirect or send json
    {:error, message} ->
      handle_error()
  end
end

In this example, the Users.confirm_user function updates the database, setting the confirmed_at value to the current time.

Password resetting

For password resetting, use Phauxth.Confirm.PassReset.verify, as in the following example:

def update(conn, %{"password_reset" => params}) do
  case Phauxth.Confirm.PassReset.verify(params) do
    {:ok, user} ->
      Users.update_password(user, params)
      |> handle_password_reset(conn, params)
    {:error, message} ->
      handle_error()
  end
end

The Users.update_password function tries to add the new password to the database. If the password reset is successful, the handle_password_reset function sends a message by email to the user and redirects the user to the next page or sends a json response. If unsuccessful, the handle_password_reset function handles the error.