- Remove auto-confirmation on registration (register_user and register_user_with_organization) - Add deliver_user_confirmation_instructions/2 and confirm_user/1 to Accounts context - Add verify_email_token_query/2 and by_user_and_contexts_query/2 to UserToken - Make deliver_confirmation_email public in UserNotifier - Send confirmation email after registration in both flows - Block unconfirmed users at password login with helpful message - Add UserConfirmationController with confirm/resend routes - Add resend confirmation link to login page - Update test fixtures to confirm users after registration
42 lines
1.1 KiB
Elixir
42 lines
1.1 KiB
Elixir
defmodule ToweropsWeb.UserConfirmationController do
|
|
use ToweropsWeb, :controller
|
|
|
|
alias Towerops.Accounts
|
|
|
|
def new(conn, _params) do
|
|
render(conn, :new)
|
|
end
|
|
|
|
def create(conn, %{"user" => %{"email" => email}}) do
|
|
if user = Accounts.get_user_by_email(email) do
|
|
if is_nil(user.confirmed_at) do
|
|
Accounts.deliver_user_confirmation_instructions(
|
|
user,
|
|
&url(~p"/users/confirm/#{&1}")
|
|
)
|
|
end
|
|
end
|
|
|
|
# Always show success to prevent email enumeration
|
|
conn
|
|
|> put_flash(
|
|
:info,
|
|
"If your email is in our system and has not yet been confirmed, you will receive an email with instructions shortly."
|
|
)
|
|
|> redirect(to: ~p"/users/log-in")
|
|
end
|
|
|
|
def confirm(conn, %{"token" => token}) do
|
|
case Accounts.confirm_user(token) do
|
|
{:ok, _user} ->
|
|
conn
|
|
|> put_flash(:info, "Email confirmed successfully. You may now log in.")
|
|
|> redirect(to: ~p"/users/log-in")
|
|
|
|
:error ->
|
|
conn
|
|
|> put_flash(:error, "Email confirmation link is invalid or has expired.")
|
|
|> redirect(to: ~p"/users/log-in")
|
|
end
|
|
end
|
|
end
|