Haytni.RegisterablePlugin (Haytni v0.7.0) View Source

This plugin allows the user to register and edit their account.

Change your_app/lib/your_app/user.ex to add two functions: create_registration_changeset and update_registration_changeset.

Example:

defmodule YourApp.User do
  require YourApp.Haytni

  @derive {Inspect, except: [:password]}
  schema "users" do
    YourApp.Haytni.fields()

    # ...
  end

  # ...

  # called when a user try to register himself
  def create_registration_changeset(%__MODULE__{} = struct, params) do
    struct
    |> cast(params, ~W[email password]a) # add any field you'll may need (but only fields that user is allowed to define!)
    |> YourApp.Haytni.validate_password()
    # add any custom validation here
    |> YourApp.Haytni.validate_create_registration()
  end

  # called when a user try to edit its own account (logic is completely different)
  def update_registration_changeset(%__MODULE__{} = struct, params) do
    struct
    |> cast(params, ~W[]a) # add any field in the list you'll may need (but only fields that user is allowed to redefine!)
    # add any custom validation here
    |> YourApp.Haytni.validate_update_registration()
  end

  # ...
end

Fields: none

Configuration:

  • email_regexp (default: ~r/^[^@\s]+@[^@\s]+$/): the Regex that an email at registration or profile edition needs to match

  • case_insensitive_keys (default: [:email]): list of fields to automatically downcase on registration. May be unneeded depending on your database (eg: citext columns for PostgreSQL or columns with a collation suffixed by "_ci" for MySQL). You SHOULD NOT include the password field here!

  • strip_whitespace_keys (default: [:email]): list of fields to automatically strip from whitespaces. You SHOULD NEITHER include the password field here, to exclude any involuntary mistake, you should instead consider using a custom validation.

  • email_index_name (default: nil, translated to <source>_email_index by Ecto.Changeset.unique_constraint/3): the name of the unique index/constraint on email field

  • registration_disabled? (default: false): disable any new registration (existing users are still able to login, edit their profile, ...)

  • with_delete (default: false): true to allow users to delete their own account (this mainly (en|dis)ables the route to reach the delete action of the HaytniWeb.Registerable.RegistrationController controller)

  • logout_on_deletion (default: true): when true the user is also logged out. Set it to false if the user should keep its session active and navigating as this user

    stack Haytni.RegisterablePlugin,
      registration_disabled?: false,
      strip_whitespace_keys: [:email],
      case_insensitive_keys: [:email],
      email_regexp: ~r/^[^@\s]+@[^@\s]+$/,
      email_index_name: nil

Routes:

  • haytni_<scope>_registration_path (actions: new/create, edit/update): paths used by the generated routes for this plugin can be customized on YourAppWeb.Haytni.routes/1 call in your router by the following options:
    • registration_path (default: "/registration"): the base/default path for all the actions
    • new_registration_path (default: registration_path <> "/new"): define this option to define a specific path for the new action (sign up/account creation)
    • edit_registration_path (default: registration_path <> "/edit"): same for edit action (profile edition)

Link to this section Summary

Functions

Downcase values of a changeset to keys configured as case_insensitive_keys

Returns an %Ecto.Changeset{} to delete its own account.

Returns an %Ecto.Changeset{} to modify its email address.

Returns an %Ecto.Changeset{} to modify its password.

Triggers user's account deletion, by calling Haytni.delete_user/2, if current_password matches user's password.

Callback implementation for Haytni.Plugin.fields/1.

The translated string to display when email hasn't changed

The translated string to display when user's current password is incorrect

Trim values of a changeset to keys configured as strip_whitespace_keys

Updates user's email address if current_password matches user's actual password.

Link to this section Functions

Link to this function

case_insensitive_changes(changeset, config)

View Source

Specs

case_insensitive_changes(
  changeset :: Ecto.Changeset.t(),
  config :: Haytni.RegisterablePlugin.Config.t()
) :: Ecto.Changeset.t()

Downcase values of a changeset to keys configured as case_insensitive_keys

Link to this function

change_deletion(module, config, user, attrs \\ %{})

View Source

Specs

change_deletion(
  module :: module(),
  config :: Haytni.RegisterablePlugin.Config.t(),
  user :: Haytni.user(),
  attrs :: Haytni.params()
) :: Ecto.Changeset.t()

Returns an %Ecto.Changeset{} to delete its own account.

Link to this function

change_email(module, config, user, attrs \\ %{})

View Source

Specs

change_email(
  module :: module(),
  config :: Haytni.RegisterablePlugin.Config.t(),
  user :: Haytni.user(),
  attrs :: Haytni.params()
) :: Ecto.Changeset.t()

Returns an %Ecto.Changeset{} to modify its email address.

Link to this function

change_password(module, user, attrs \\ %{})

View Source

Specs

change_password(
  module :: module(),
  user :: Haytni.user(),
  attrs :: Haytni.params()
) :: Ecto.Changeset.t()

Returns an %Ecto.Changeset{} to modify its password.

Link to this function

delete_account(module, config, user, current_password, attrs)

View Source

Specs

delete_account(
  module :: module(),
  config :: Haytni.RegisterablePlugin.Config.t(),
  user :: Haytni.user(),
  current_password :: String.t(),
  attrs :: Haytni.params()
) :: Haytni.multi_result()

Triggers user's account deletion, by calling Haytni.delete_user/2, if current_password matches user's password.

Returns the result of Haytni.delete_user/2 or {:error, :validation_failed, %Ecto.Changeset{}, %{}} if current_password is incorrect and/or user has not accepted the terms

Callback implementation for Haytni.Plugin.fields/1.

Link to this function

find_user(conn, module, config)

View Source

Callback implementation for Haytni.Plugin.find_user/3.

Link to this function

has_not_changed_message()

View Source

Specs

has_not_changed_message() :: String.t()

The translated string to display when email hasn't changed

Link to this function

invalid?(user, module, config)

View Source

Callback implementation for Haytni.Plugin.invalid?/3.

Link to this function

invalid_current_password_message()

View Source

Specs

invalid_current_password_message() :: String.t()

The translated string to display when user's current password is incorrect

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_email_change(multi, changeset, module, config)

View Source

Callback implementation for Haytni.Plugin.on_email_change/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_registration(multi, module, config)

View Source

Callback implementation for Haytni.Plugin.on_registration/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

strip_whitespace_changes(changeset, config)

View Source

Specs

strip_whitespace_changes(
  changeset :: Ecto.Changeset.t(),
  config :: Haytni.RegisterablePlugin.Config.t()
) :: Ecto.Changeset.t()

Trim values of a changeset to keys configured as strip_whitespace_keys

Link to this function

update_email(module, config, user, current_password, attrs)

View Source

Specs

update_email(
  module :: module(),
  config :: Haytni.RegisterablePlugin.Config.t(),
  user :: Haytni.user(),
  current_password :: String.t(),
  attrs :: Haytni.params()
) :: Haytni.repo_nobang_operation(Haytni.user())

Updates user's email address if current_password matches user's actual password.

Link to this function

update_password(module, user, current_password, attrs)

View Source

Specs

update_password(
  module :: module(),
  user :: Haytni.user(),
  current_password :: String.t(),
  attrs :: Haytni.params()
) :: Haytni.repo_nobang_operation(Haytni.user())

Updates user's password if:

  • current_password matches user's actual password
  • the new password meets the requirements against the active plugins implementing the Haytni.Plugin.validate_password/3 callback

When the password is changed, the tokens associated to user are also deleted.

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.