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]+$/
): theRegex
that an email at registration or profile edition needs to matchcase_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
byEcto.Changeset.unique_constraint/3
): the name of the unique index/constraint on email fieldregistration_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 theHaytniWeb.Registerable.RegistrationController
controller)logout_on_deletion
(default:true
): whentrue
the user is also logged out. Set it tofalse
if the user should keep its session active and navigating as this userstack 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)
- registration_path (default:
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
.
Callback implementation for Haytni.Plugin.find_user/3
.
The translated string to display when email hasn't changed
Callback implementation for Haytni.Plugin.invalid?/3
.
The translated string to display when user's current password is incorrect
Callback implementation for Haytni.Plugin.on_delete_user/4
.
Callback implementation for Haytni.Plugin.on_email_change/4
.
Callback implementation for Haytni.Plugin.on_failed_authentication/5
.
Callback implementation for Haytni.Plugin.on_logout/3
.
Callback implementation for Haytni.Plugin.on_registration/3
.
Callback implementation for Haytni.Plugin.on_successful_authentication/6
.
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.
Updates user's password if
Callback implementation for Haytni.Plugin.validate_password/3
.
Callback implementation for Haytni.Plugin.validate_update_registration/3
.
Link to this section Functions
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
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.
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.
Specs
change_password( module :: module(), user :: Haytni.user(), attrs :: Haytni.params() ) :: Ecto.Changeset.t()
Returns an %Ecto.Changeset{}
to modify its password.
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
.
Callback implementation for Haytni.Plugin.find_user/3
.
Specs
has_not_changed_message() :: String.t()
The translated string to display when email hasn't changed
Callback implementation for Haytni.Plugin.invalid?/3
.
Specs
invalid_current_password_message() :: String.t()
The translated string to display when user's current password is incorrect
Callback implementation for Haytni.Plugin.on_delete_user/4
.
Callback implementation for Haytni.Plugin.on_email_change/4
.
Callback implementation for Haytni.Plugin.on_failed_authentication/5
.
Callback implementation for Haytni.Plugin.on_logout/3
.
Callback implementation for Haytni.Plugin.on_registration/3
.
on_successful_authentication(conn, user, multi, keywords, module, config)
View SourceCallback implementation for Haytni.Plugin.on_successful_authentication/6
.
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
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.
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.
Callback implementation for Haytni.Plugin.validate_password/3
.
Callback implementation for Haytni.Plugin.validate_update_registration/3
.