mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 11:42:07 +00:00
password reset flow
This commit is contained in:
@@ -251,6 +251,48 @@ defmodule Chesstrainer.Accounts do
|
||||
|> update_user_and_delete_all_tokens()
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
Delivers the reset password email instructions to the given user.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> deliver_user_reset_password_instructions(user, &url(~p"/users/reset-password/#{&1}"))
|
||||
{:ok, %{to: ..., body: ...}}
|
||||
|
||||
"""
|
||||
def deliver_user_reset_password_instructions(%User{} = user, reset_password_url_fun)
|
||||
when is_function(reset_password_url_fun, 1) do
|
||||
{encoded_token, user_token} = UserToken.build_email_token(user, "reset_password")
|
||||
Repo.insert!(user_token)
|
||||
UserNotifier.deliver_reset_password_instructions(user, reset_password_url_fun.(encoded_token))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets the user with the given reset password token.
|
||||
|
||||
Returns `nil` if the token is invalid or has expired.
|
||||
"""
|
||||
def get_user_by_reset_password_token(token) do
|
||||
with {:ok, query} <- UserToken.verify_reset_password_token_query(token),
|
||||
%UserToken{sent_to: email} <- Repo.one(query),
|
||||
%User{} = user <- Repo.get_by(User, email: email) do
|
||||
user
|
||||
else
|
||||
_ -> nil
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Resets the user password.
|
||||
|
||||
Returns a tuple with the updated user, as well as a list of expired tokens.
|
||||
"""
|
||||
def reset_user_password(user, attrs) do
|
||||
user
|
||||
|> User.password_changeset(attrs)
|
||||
|> update_user_and_delete_all_tokens()
|
||||
end
|
||||
|
||||
## Deletion
|
||||
|
||||
@doc """
|
||||
|
||||
@@ -37,6 +37,26 @@ defmodule Chesstrainer.Accounts.UserNotifier do
|
||||
""")
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deliver instructions to reset a user password.
|
||||
"""
|
||||
def deliver_reset_password_instructions(user, url) do
|
||||
deliver(user.email, "Reset password instructions", """
|
||||
|
||||
==============================
|
||||
|
||||
Hi #{user.email},
|
||||
|
||||
You can reset your password by visiting the URL below:
|
||||
|
||||
#{url}
|
||||
|
||||
If you didn't request this change, please ignore this.
|
||||
|
||||
==============================
|
||||
""")
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deliver instructions to update a user email.
|
||||
"""
|
||||
|
||||
@@ -8,6 +8,7 @@ defmodule Chesstrainer.Accounts.UserToken do
|
||||
|
||||
@change_email_validity_in_days 7
|
||||
@confirm_validity_in_days 1
|
||||
@reset_password_validity_in_days 1
|
||||
@session_validity_in_days 14
|
||||
|
||||
@primary_key {:id, :binary_id, autogenerate: true}
|
||||
@@ -147,6 +148,31 @@ defmodule Chesstrainer.Accounts.UserToken do
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks if the token is valid and returns its underlying lookup query.
|
||||
|
||||
The query returns the user_token found by the token, if any.
|
||||
|
||||
This is used to validate requests to reset the user password.
|
||||
The given token is valid if it matches its hashed counterpart in the
|
||||
database and if it has not expired (after @reset_password_validity_in_days).
|
||||
"""
|
||||
def verify_reset_password_token_query(token) do
|
||||
case Base.url_decode64(token, padding: false) do
|
||||
{:ok, decoded_token} ->
|
||||
hashed_token = :crypto.hash(@hash_algorithm, decoded_token)
|
||||
|
||||
query =
|
||||
from token in by_token_and_context_query(hashed_token, "reset_password"),
|
||||
where: token.inserted_at > ago(@reset_password_validity_in_days, "day")
|
||||
|
||||
{:ok, query}
|
||||
|
||||
:error ->
|
||||
:error
|
||||
end
|
||||
end
|
||||
|
||||
defp by_token_and_context_query(token, context) do
|
||||
from UserToken, where: [token: ^token, context: ^context]
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user