Haytni.ConfirmablePlugin (Haytni v0.7.0) View Source

This plugin ensure that email addresses given by users are valid by sending them an email containing an unique token that they have to return back in order to really be able to use (unlock) their account.

On an email address change, it also warns the user by sending an email to the previous address and requests a confirmation, same as registering, to active in order to validate the change.

Fields:

  • confirmed_at (datetime@utc, nullable, default: NULL): when the account was confirmed else NULL

Configuration:

  • reconfirmable (default: true): any email changes have to be confirmed to be applied

  • confirmation_keys (default: [:email]): the key(s) to be matched before sending a new confirmation

  • confirm_within (default: {3, :day}): delay after which confirmation token is considered as expired (ie the user has to ask for a new one)

    stack Haytni.ConfirmablePlugin,
      confirm_within: {3, :day},
      confirmation_keys: [:email],
      reconfirmable: true,
      reconfirm_within: {3, :day}

Routes:

  • haytni_<scope>_confirmation_path (actions: show, new/create): default path is "/confirmation" but it can be redefined by the :confirmation_path option when calling YourApp.Haytni.routes/1 from your own router (eg: YourApp.Haytni.routes(confirmation_path: "/verification"))
  • haytni_<scope>_reconfirmation_path (actions: show): default path is "/reconfirmation" (overridable by the option :reconfirmation_path)

Link to this section Summary

Functions

Confirms an account from its confirmation token.

Allows a privilegied user (administrator) to manually confirm a user.

This function converts the parameters received by the controller to request a new confirmation token sent by email to an %Ecto.Changeset{}, a convenient way to perform basic validations, any intermediate handling and casting.

Has the given user been confirmed?

The (database) attribute(s) as a keyword-list (field name: new value) to update a user as a confirmed account

The translated string to display when (re)confirmation token is invalid (meaning matches no one)

The translated string to display when account is on pending (re)confirmation.

Reconfirms (validates an email address after its change) an account from its confirmation token.

Resend confirmation instructions to an email address (requested by its owner).

Link to this section Functions

Link to this function

confirm(module, config, token)

View Source

Specs

confirm(
  module :: module(),
  config :: Haytni.ConfirmablePlugin.Config.t(),
  token :: String.t()
) :: {:ok, Haytni.user()} | {:error, String.t()}

Confirms an account from its confirmation token.

Returns {:error, reason} if token is expired or invalid else the (updated) user as {:ok, user}.

Link to this function

confirm_user(module, user)

View Source

Specs

confirm_user(module :: module(), user :: Haytni.user()) ::
  Haytni.repo_nobang_operation(Haytni.user())

Allows a privilegied user (administrator) to manually confirm a user.

Example: you could add a route in your administration panel:

scope "/admin" do
  pipe_through ~W[browser restricted_to_admin]a

  resources "/users" do
    resources "/confirm", YourAppWeb.Admin.User.ConfirmController, singleton: true, only: ~W[update]a
  end
end

With the above controller calling this function:

defmodule YourAppWeb.Admin.User.ConfirmController do
  def update(conn, %{"user_id" => user_id}) do
    user = YourApp.UserContext.get_user!(user_id)
    {:ok, user} = Haytni.ConfirmablePlugin.confirm_user(YourAppWeb.Haytni, user)

    conn
    |> put_flash(:info, "user has been confirmed")
    |> redirect(to: Routes.admin_user_path(conn, :index))
    |> halt()
  end
end

And do the link in your templates with:

Status: <%= if Haytni.ConfirmablePlugin.confirmed?(user) do %>
  Confirmed
<% else %>
  Not confirmed (<%= link "force confirmation?", to: Routes.admin_user_confirm_path(@conn, user, :update) %>)
<% end %>
Link to this function

confirmation_request_changeset(config, confirmation_params \\ %{})

View Source

Specs

confirmation_request_changeset(
  config :: Haytni.ConfirmablePlugin.Config.t(),
  confirmation_params :: Haytni.params()
) :: Ecto.Changeset.t()

This function converts the parameters received by the controller to request a new confirmation token sent by email to an %Ecto.Changeset{}, a convenient way to perform basic validations, any intermediate handling and casting.

Specs

confirmed?(user :: Haytni.user()) :: boolean()

Has the given user been confirmed?

Specs

confirmed_attributes() :: Keyword.t()

The (database) attribute(s) as a keyword-list (field name: new value) to update a user as a confirmed account

Link to this function

find_user(conn, module, config)

View Source

Callback implementation for Haytni.Plugin.find_user/3.

Specs

invalid_token_message() :: String.t()

The translated string to display when (re)confirmation token is invalid (meaning matches no one)

Link to this function

on_delete_user(multi, user, module, config)

View Source

Callback implementation for Haytni.Plugin.on_delete_user/4.

Link to this function

on_failed_authentication(user, multi, keywords, module, config)

View Source

Callback implementation for Haytni.Plugin.on_failed_authentication/5.

Link to this function

on_logout(conn, module, config)

View Source

Callback implementation for Haytni.Plugin.on_logout/3.

Link to this function

on_successful_authentication(conn, user, multi, keywords, module, config)

View Source

Callback implementation for Haytni.Plugin.on_successful_authentication/6.

Link to this function

pending_confirmation_message()

View Source

Specs

pending_confirmation_message() :: String.t()

The translated string to display when account is on pending (re)confirmation.

Link to this function

reconfirm(module, config, user, token)

View Source

Specs

reconfirm(
  module :: module(),
  config :: Haytni.ConfirmablePlugin.Config.t(),
  user :: Haytni.user(),
  token :: String.t()
) :: {:ok, Haytni.user()} | {:error, String.t()}

Reconfirms (validates an email address after its change) an account from its confirmation token.

Returns {:error, reason} if token is expired or invalid else the (updated) user as {:ok, user}.

Link to this function

resend_confirmation_instructions(module, config, confirmation_params)

View Source

Specs

resend_confirmation_instructions(
  module :: module(),
  config :: Haytni.ConfirmablePlugin.Config.t(),
  confirmation_params :: Haytni.params()
) :: {:ok, Haytni.nilable(Haytni.Token.t())} | {:error, Ecto.Changeset.t()}

Resend confirmation instructions to an email address (requested by its owner).

Returns:

  • {:ok, token}: a token was actualy sent by mail
  • {:ok, nil}: there is no account matching config.confirmation_keys or the account is not pending confirmation
  • {:error, changeset}: fields (form) was invalid
Link to this function

validate_create_registration(changeset, module, config)

View Source

Callback implementation for Haytni.Plugin.validate_create_registration/3.

Link to this function

validate_password(changeset, module, config)

View Source

Callback implementation for Haytni.Plugin.validate_password/3.

Link to this function

validate_update_registration(changeset, module, config)

View Source

Callback implementation for Haytni.Plugin.validate_update_registration/3.