email signup up flow in dev mode, therefore in place for prod

This commit is contained in:
2026-11-05 14:11:12 +01:00
parent 6b0e0ed352
commit e89df68e7d
12 changed files with 271 additions and 11 deletions
+42 -3
View File
@@ -98,9 +98,6 @@ defmodule Chesstrainer.Accounts do
def register_user(attrs) do
%User{}
|> User.registration_changeset(attrs)
# Accounts are usable straight away; email confirmation is intentionally off
# because no production mailer adapter is configured yet.
|> Ecto.Changeset.put_change(:confirmed_at, DateTime.utc_now(:second))
|> Repo.insert()
end
@@ -113,6 +110,48 @@ defmodule Chesstrainer.Accounts do
User.registration_changeset(user, attrs, opts)
end
@doc ~S"""
Delivers the confirmation email instructions to the given user.
## Examples
iex> deliver_user_confirmation_instructions(user, &url(~p"/users/confirm/#{&1}"))
{:ok, %{to: ..., body: ...}}
iex> deliver_user_confirmation_instructions(confirmed_user, &url(~p"/users/confirm/#{&1}"))
{:error, :already_confirmed}
"""
def deliver_user_confirmation_instructions(%User{} = user, confirmation_url_fun)
when is_function(confirmation_url_fun, 1) do
if user.confirmed_at do
{:error, :already_confirmed}
else
{encoded_token, user_token} = UserToken.build_email_token(user, "confirm")
Repo.insert!(user_token)
UserNotifier.deliver_confirmation_instructions(user, confirmation_url_fun.(encoded_token))
end
end
@doc """
Confirms a user by the given token.
If the token matches, the user account is marked as confirmed and the token is
deleted. The token is valid for one day.
"""
def confirm_user(token) do
with {:ok, query} <- UserToken.verify_confirmation_token_query(token),
%UserToken{sent_to: email} <- Repo.one(query),
%User{} = user <- Repo.get_by(User, email: email),
{:ok, user} <- Repo.update(User.confirm_changeset(user)),
{_count, _result} <-
Repo.delete_all(from(UserToken, where: [user_id: ^user.id, context: "confirm"])) do
{:ok, user}
else
_ -> :error
end
end
@doc """
Updates a user's authority role.
@@ -17,6 +17,26 @@ defmodule Chesstrainer.Accounts.UserNotifier do
end
end
@doc """
Deliver instructions to confirm account.
"""
def deliver_confirmation_instructions(user, url) do
deliver(user.email, "Confirmation instructions", """
==============================
Hi #{user.email},
You can confirm your account by visiting the URL below:
#{url}
If you didn't create an account with us, please ignore this.
==============================
""")
end
@doc """
Deliver instructions to update a user email.
"""
+26
View File
@@ -7,6 +7,7 @@ defmodule Chesstrainer.Accounts.UserToken do
@rand_size 32
@change_email_validity_in_days 7
@confirm_validity_in_days 1
@session_validity_in_days 14
@primary_key {:id, :binary_id, autogenerate: true}
@@ -121,6 +122,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 confirm the user account.
The given token is valid if it matches its hashed counterpart in the
database and if it has not expired (after @confirm_validity_in_days).
"""
def verify_confirmation_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, "confirm"),
where: token.inserted_at > ago(@confirm_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