mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 03:32:06 +00:00
email signup up flow in dev mode, therefore in place for prod
This commit is contained in:
@@ -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.
|
||||
"""
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
defmodule ChesstrainerWeb.UserConfirmationController do
|
||||
use ChesstrainerWeb, :controller
|
||||
|
||||
alias Chesstrainer.Accounts
|
||||
|
||||
def edit(conn, %{"token" => token}) do
|
||||
case Accounts.confirm_user(token) do
|
||||
{:ok, _user} ->
|
||||
conn
|
||||
|> put_flash(:info, "User confirmed successfully.")
|
||||
|> redirect(to: ~p"/users/log-in")
|
||||
|
||||
:error ->
|
||||
conn
|
||||
|> put_flash(:error, "User not found or token is invalid.")
|
||||
|> redirect(to: ~p"/users/register")
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -17,9 +17,24 @@ defmodule ChesstrainerWeb.UserSessionController do
|
||||
|> redirect(to: ~p"/users/log-in")
|
||||
|
||||
user ->
|
||||
conn
|
||||
|> put_flash(:info, "Welcome back!")
|
||||
|> UserAuth.log_in_user(user, user_params)
|
||||
if user.confirmed_at do
|
||||
conn
|
||||
|> put_flash(:info, "Welcome back!")
|
||||
|> UserAuth.log_in_user(user, user_params)
|
||||
else
|
||||
{:ok, _} =
|
||||
Accounts.deliver_user_confirmation_instructions(
|
||||
user,
|
||||
&url(~p"/users/confirm/#{&1}")
|
||||
)
|
||||
|
||||
# In order to prevent user enumeration attacks, report the same error
|
||||
# as invalid credentials.
|
||||
conn
|
||||
|> put_flash(:error, "Invalid email, username or password")
|
||||
|> put_flash(:email, String.slice(login, 0, 160))
|
||||
|> redirect(to: ~p"/users/log-in")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -81,10 +81,19 @@ defmodule ChesstrainerWeb.UserLive.Registration do
|
||||
@impl true
|
||||
def handle_event("save", %{"user" => user_params}, socket) do
|
||||
case Accounts.register_user(user_params) do
|
||||
{:ok, _user} ->
|
||||
{:ok, user} ->
|
||||
{:ok, _} =
|
||||
Accounts.deliver_user_confirmation_instructions(
|
||||
user,
|
||||
&url(~p"/users/confirm/#{&1}")
|
||||
)
|
||||
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:info, "Account created successfully. Please log in.")
|
||||
|> put_flash(
|
||||
:info,
|
||||
"An email with instructions for how to confirm your email address has been sent to your inbox."
|
||||
)
|
||||
|> push_navigate(to: ~p"/users/log-in")}
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
|
||||
@@ -85,6 +85,8 @@ defmodule ChesstrainerWeb.Router do
|
||||
live "/users/log-in", UserLive.Login, :new
|
||||
end
|
||||
|
||||
get "/users/confirm/:token", UserConfirmationController, :edit
|
||||
|
||||
post "/users/log-in", UserSessionController, :create
|
||||
delete "/users/log-out", UserSessionController, :delete
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user