Haytni v0.0.1 Haytni.LockablePlugin View Source
This plugin locks an account after a specified number of failed sign-in attempts. User can unlock its account via email or after a specified time period.
Fields:
- failed_attempts (integer, default:
0
): the current count of successive failures to login - locked_at (datetime@utc, nullable, default:
NULL
): when the account was locked (NULL
while the account is not locked) - unlock_token (string, nullable, unique, default:
NULL
): the token send to the user to unlock its account
Configuration:
maximum_attempts
(default:20
): the amount of successive attempts to login before locking the corresponding accountunlock_token_length
(default:32
): the length of the generated tokenunlock_keys
(default:~W[email]a
): the field(s) to match to accept the unlock requestunlock_in
(default:{1, :hour}
): delay to automatically unlock the accountunlock_strategy
(default::both
): strategy used to unlock an account. One of::email
: sends an unlock link to the user email:time
: re-enables login after a certain amount of time (see :unlock_in below):both
: enables both strategies:none
: no unlock strategy. You should handle unlocking by yourself.
Routes:
unlock_path
(actions: new/create, show)
Link to this section Summary
Functions
Returns true
if :email
(included in :both
) is enabled
Extract (early) the user from the HTTP request (http authentification, cookies/session, …)
Returns true
if it’s the last attempt before account locking in case of a new sign-in failure
Returns true
if user account is currently locked
This callback is invoked when a user is editing its registration and change its email address.
It is a facility (subset) to avoid you to handle it by yourself via validate_update_registration/1
This callback is invoked when a user (manually) log out. Its purpose is mainly to do some cleanup like removing a cookie
Invoked to accomplish a task right after user’s registration (insert). This callback allows you to do some linked changes to the database, send an email or whatever by appending it to multi
Resend, by email, the instructions to unlock an account
Unlock an account from a token
This callback let you do any kind of change or additionnal validation on the changeset when a user is registering
Same as validate_create_registration
but registration’s edition as logic between the two
may be completely different
Link to this section Functions
Returns true
if :email
(included in :both
) is enabled
Extract (early) the user from the HTTP request (http authentification, cookies/session, …).
Returns a tuple of the form {conn, user}
with user being nil
if no user could be found at
this early stage.
Callback implementation for Haytni.Plugin.find_user/1
.
Returns true
if it’s the last attempt before account locking in case of a new sign-in failure
Returns true
if user account is currently locked.
This callback is invoked when a user is editing its registration and change its email address.
It is a facility (subset) to avoid you to handle it by yourself via validate_update_registration/1
.
It returns a tuple of {Ecto.Multi, Ecto.Changeset}
, same as its arguments, to permit to the
callback to add any operation to multi or change to changeset.
This callback is called before updating the user but the actions added to multi will be run after its update.
Callback implementation for Haytni.Plugin.on_email_change/2
.
This callback is invoked when a user (manually) log out. Its purpose is mainly to do some cleanup like removing a cookie.
Callback implementation for Haytni.Plugin.on_logout/1
.
Invoked to accomplish a task right after user’s registration (insert). This callback allows you to do some linked changes to the database, send an email or whatever by appending it to multi.
Remember to comply to Ecto.Multi
functions. In particular Ecto.Multi.run
: the function
called by it have to return {:ok, your value}
or {:error, your value}
. Also note that
the inserted user will be passed to the function called by Ecto.Multi.run
as the :user
key to the map received by the last one as its (only) argument.
The following example illustrate how to send a welcome mail:
def on_registration(multi = %Ecto.Multi{}) do
multi
|> Ecto.Multi.run(:send_welcome_email, fn %{user: user} ->
send_welcome_email_to(user)
{:ok, :success}
end)
end
Callback implementation for Haytni.Plugin.on_registration/1
.
resend_unlock_instructions(request :: Haytni.Unlockable.Request.t()) :: {:ok, struct()} | {:error, :no_match | :not_locked | :email_strategy_disabled}
Resend, by email, the instructions to unlock an account.
Returns:
{:error, :email_strategy_disabled}
if:email
strategy is disabled{:error, :no_match}
if there is no such account matchingunlock_keys
{:error, :not_locked}
if the account is not currently locked{:ok, user}
if successful
Unlock an account from a token.
Returns the user if the token exists and {:error, message}
if not.
Also raises if updating user fails.
This callback let you do any kind of change or additionnal validation on the changeset when a user is registering.
Callback implementation for Haytni.Plugin.validate_create_registration/1
.
Same as validate_create_registration
but registration’s edition as logic between the two
may be completely different.
Callback implementation for Haytni.Plugin.validate_update_registration/1
.