mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 11:42:07 +00:00
add mailing list signup with email confirmation (#67)
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
defmodule ChesstrainerWeb.MailingListController do
|
||||
use ChesstrainerWeb, :controller
|
||||
|
||||
alias Chesstrainer.MailingList
|
||||
alias Chesstrainer.MailingList.Notifier
|
||||
|
||||
@return_to "/#mailing-list"
|
||||
@token_salt "mailing_list_confirm"
|
||||
@token_max_age 7 * 24 * 60 * 60
|
||||
|
||||
def create(conn, %{"subscriber" => %{"email" => email}}) when is_binary(email) do
|
||||
email = String.trim(email)
|
||||
|
||||
case MailingList.subscribe(email) do
|
||||
{:ok, :already_subscribed} ->
|
||||
conn
|
||||
|> put_flash(:info, "You're already on the mailing list.")
|
||||
|> redirect(to: @return_to)
|
||||
|
||||
{:ok, :confirmation_required} ->
|
||||
case send_confirmation(email) do
|
||||
:ok ->
|
||||
conn
|
||||
|> put_flash(:info, "Check your inbox to confirm your subscription.")
|
||||
|> redirect(to: @return_to)
|
||||
|
||||
:error ->
|
||||
conn
|
||||
|> put_flash(:error, "We couldn't send the confirmation email. Please try again.")
|
||||
|> redirect(to: @return_to)
|
||||
end
|
||||
|
||||
{:error, %Ecto.Changeset{}} ->
|
||||
conn
|
||||
|> put_flash(:error, "Enter a valid email address.")
|
||||
|> redirect(to: @return_to)
|
||||
end
|
||||
end
|
||||
|
||||
def create(conn, _params) do
|
||||
conn
|
||||
|> put_flash(:error, "Enter a valid email address.")
|
||||
|> redirect(to: @return_to)
|
||||
end
|
||||
|
||||
def confirm(conn, %{"token" => token}) do
|
||||
case Phoenix.Token.verify(
|
||||
ChesstrainerWeb.Endpoint,
|
||||
@token_salt,
|
||||
token,
|
||||
max_age: @token_max_age
|
||||
) do
|
||||
{:ok, email} ->
|
||||
confirm_subscription(conn, email)
|
||||
|
||||
{:error, _reason} ->
|
||||
conn
|
||||
|> put_flash(:error, "That confirmation link is invalid or has expired.")
|
||||
|> redirect(to: @return_to)
|
||||
end
|
||||
end
|
||||
|
||||
defp confirm_subscription(conn, email) do
|
||||
case MailingList.confirm(email) do
|
||||
{:ok, _subscriber} ->
|
||||
conn
|
||||
|> put_flash(:info, "Your mailing list subscription is confirmed.")
|
||||
|> redirect(to: @return_to)
|
||||
|
||||
{:error, :not_found} ->
|
||||
conn
|
||||
|> put_flash(
|
||||
:error,
|
||||
"That address is no longer on the mailing list. Please sign up again."
|
||||
)
|
||||
|> redirect(to: @return_to)
|
||||
end
|
||||
end
|
||||
|
||||
defp send_confirmation(email) do
|
||||
token = Phoenix.Token.sign(ChesstrainerWeb.Endpoint, @token_salt, email)
|
||||
url = url(~p"/mailing-list/confirm/#{token}")
|
||||
|
||||
case Notifier.deliver_confirmation_instructions(email, url) do
|
||||
{:ok, _email} -> :ok
|
||||
{:error, _reason} -> :error
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -71,6 +71,8 @@
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<.mailing_list_signup />
|
||||
|
||||
<%!-- <section class="progress-banner" id="progress">
|
||||
<div>
|
||||
<span class="section-kicker">A calmer way to improve</span>
|
||||
|
||||
@@ -24,6 +24,13 @@
|
||||
We record the puzzles you attempt and whether you solved them, so we can
|
||||
show your progress and keep the daily puzzle meaningful.
|
||||
</li>
|
||||
<li>
|
||||
<strong>Mailing list email.</strong>
|
||||
If you join the mailing list — using the form on the home page or the
|
||||
checkbox when registering — we store your email address and where you
|
||||
signed up, so we can send you occasional updates. You can ask to be
|
||||
removed at any time.
|
||||
</li>
|
||||
<li>
|
||||
<strong>Technical data.</strong>
|
||||
Our hosting and server infrastructure may keep standard request logs
|
||||
@@ -58,6 +65,7 @@
|
||||
<li>To create and secure your account and sign you in.</li>
|
||||
<li>To run the trainer and remember your progress.</li>
|
||||
<li>To send service emails, such as confirmation and password reset links.</li>
|
||||
<li>To send occasional endgame tips and site news, if you opted in.</li>
|
||||
<li>To keep the site available, prevent abuse, and fix faults.</li>
|
||||
<li>To respond when you ask for help.</li>
|
||||
</ul>
|
||||
|
||||
@@ -2,10 +2,13 @@ defmodule ChesstrainerWeb.UserConfirmationController do
|
||||
use ChesstrainerWeb, :controller
|
||||
|
||||
alias Chesstrainer.Accounts
|
||||
alias Chesstrainer.MailingList
|
||||
|
||||
def edit(conn, %{"token" => token}) do
|
||||
case Accounts.confirm_user(token) do
|
||||
{:ok, _user} ->
|
||||
{:ok, user} ->
|
||||
MailingList.confirm_user_subscription(user)
|
||||
|
||||
conn
|
||||
|> put_flash(:info, "User confirmed successfully.")
|
||||
|> redirect(to: ~p"/users/log-in")
|
||||
|
||||
Reference in New Issue
Block a user