Haytni.RegisterablePlugin (Haytni v0.6.3) 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[email password current_password]a) # add any field 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, ...)

    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

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

find_user(conn, module, config)

View Source

Callback implementation for Haytni.Plugin.find_user/3.

Link to this function

invalid?(user, module, config)

View Source

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

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

validate_password(changeset, module, config)

View Source

Callback implementation for Haytni.Plugin.validate_password/3.