mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 03:32:06 +00:00
prelaunch env var prevents the site from being operational
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
defmodule ChesstrainerWeb.HealthController do
|
||||
@moduledoc """
|
||||
Lightweight health check used by deploys and uptime monitors.
|
||||
"""
|
||||
|
||||
use ChesstrainerWeb, :controller
|
||||
|
||||
def index(conn, _params) do
|
||||
conn
|
||||
|> put_resp_content_type("text/plain")
|
||||
|> send_resp(200, "ok")
|
||||
end
|
||||
end
|
||||
@@ -10,24 +10,16 @@ defmodule ChesstrainerWeb.MailingListController do
|
||||
@token_max_age 7 * 24 * 60 * 60
|
||||
|
||||
def create(conn, %{"subscriber" => params}) when is_map(params) do
|
||||
email = params["email"]
|
||||
|
||||
cond do
|
||||
honeypot_filled?(params) ->
|
||||
fake_success(conn)
|
||||
|
||||
not is_binary(email) ->
|
||||
invalid_email(conn)
|
||||
|
||||
MailingListFormToken.verify(params["form_token"]) != :ok ->
|
||||
form_error(conn)
|
||||
|
||||
true ->
|
||||
subscribe(conn, email)
|
||||
if mailing_list_enabled?() do
|
||||
create_subscription(conn, params)
|
||||
else
|
||||
closed(conn)
|
||||
end
|
||||
end
|
||||
|
||||
def create(conn, _params), do: invalid_email(conn)
|
||||
def create(conn, _params) do
|
||||
if mailing_list_enabled?(), do: invalid_email(conn), else: closed(conn)
|
||||
end
|
||||
|
||||
def confirm(conn, %{"token" => token}) do
|
||||
case Phoenix.Token.verify(
|
||||
@@ -63,6 +55,24 @@ defmodule ChesstrainerWeb.MailingListController do
|
||||
end
|
||||
end
|
||||
|
||||
defp create_subscription(conn, params) do
|
||||
email = params["email"]
|
||||
|
||||
cond do
|
||||
honeypot_filled?(params) ->
|
||||
fake_success(conn)
|
||||
|
||||
not is_binary(email) ->
|
||||
invalid_email(conn)
|
||||
|
||||
MailingListFormToken.verify(params["form_token"]) != :ok ->
|
||||
form_error(conn)
|
||||
|
||||
true ->
|
||||
subscribe(conn, email)
|
||||
end
|
||||
end
|
||||
|
||||
defp subscribe(conn, email) do
|
||||
email = String.trim(email)
|
||||
|
||||
@@ -122,4 +132,14 @@ defmodule ChesstrainerWeb.MailingListController do
|
||||
|> put_flash(:error, "Enter a valid email address.")
|
||||
|> redirect(to: @return_to)
|
||||
end
|
||||
|
||||
defp closed(conn) do
|
||||
conn
|
||||
|> put_flash(:info, "The mailing list isn't open yet.")
|
||||
|> redirect(to: @return_to)
|
||||
end
|
||||
|
||||
defp mailing_list_enabled? do
|
||||
Application.get_env(:chesstrainer, :mailing_list_enabled, true)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -11,22 +11,27 @@ defmodule ChesstrainerWeb.PageController do
|
||||
"Learn to see the winning move and make the endgame your strongest phase."
|
||||
|
||||
def home(conn, _params) do
|
||||
endgames = Endgames.curated_endgames()
|
||||
if prelaunch?() do
|
||||
render_coming_soon(conn)
|
||||
else
|
||||
endgames = Endgames.curated_endgames()
|
||||
|
||||
conn
|
||||
|> assign(:js_entry, ~p"/assets/js/marketing.js")
|
||||
|> assign(:page_title, Branding.home_title())
|
||||
|> assign(:title_suffix, "")
|
||||
|> assign(:meta_description, @description)
|
||||
|> assign(:canonical_url, current_url(conn))
|
||||
|> render(:home,
|
||||
endgames: endgames,
|
||||
active_endgame: DailyPuzzles.today(),
|
||||
puzzle_count: Endgames.count_endgames(),
|
||||
user_count: Attempts.count_users_with_attempts(),
|
||||
logged_in_count: Accounts.count_logged_in_users(),
|
||||
today: Date.utc_today()
|
||||
)
|
||||
conn
|
||||
|> assign(:js_entry, ~p"/assets/js/marketing.js")
|
||||
|> assign(:page_title, Branding.home_title())
|
||||
|> assign(:title_suffix, "")
|
||||
|> assign(:meta_description, @description)
|
||||
|> assign(:canonical_url, current_url(conn))
|
||||
|> assign(:mailing_list_enabled, mailing_list_enabled?())
|
||||
|> render(:home,
|
||||
endgames: endgames,
|
||||
active_endgame: DailyPuzzles.today(),
|
||||
puzzle_count: Endgames.count_endgames(),
|
||||
user_count: Attempts.count_users_with_attempts(),
|
||||
logged_in_count: Accounts.count_logged_in_users(),
|
||||
today: Date.utc_today()
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
def privacy(conn, _params) do
|
||||
@@ -61,4 +66,22 @@ defmodule ChesstrainerWeb.PageController do
|
||||
|> assign(:noindex, true)
|
||||
|> render(:not_found)
|
||||
end
|
||||
|
||||
defp render_coming_soon(conn) do
|
||||
conn
|
||||
|> assign(:js_entry, ~p"/assets/js/marketing.js")
|
||||
|> assign(:page_title, Branding.home_title())
|
||||
|> assign(:title_suffix, "")
|
||||
|> assign(:meta_description, @description)
|
||||
|> assign(:canonical_url, current_url(conn))
|
||||
|> assign(:noindex, true)
|
||||
|> assign(:mailing_list_enabled, mailing_list_enabled?())
|
||||
|> render(:coming_soon)
|
||||
end
|
||||
|
||||
defp prelaunch?, do: Application.get_env(:chesstrainer, :prelaunch, false)
|
||||
|
||||
defp mailing_list_enabled? do
|
||||
Application.get_env(:chesstrainer, :mailing_list_enabled, true)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
<Layouts.flash_group flash={@flash} />
|
||||
<main id="main-content" tabindex="-1">
|
||||
<section class="hero">
|
||||
<div class="hero-copy">
|
||||
<div class="eyebrow">
|
||||
<.icon_spark /> Endgame training, coming soon
|
||||
</div>
|
||||
<h1>
|
||||
Master the <em>endgame.</em>
|
||||
</h1>
|
||||
<p>
|
||||
Short, focused endgame puzzles against tablebase-perfect defence. We're putting the finishing touches on the trainer.
|
||||
</p>
|
||||
<div :if={@mailing_list_enabled} class="hero-actions">
|
||||
<.pill_button variant={:primary} href="#mailing-list">Join the list</.pill_button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="board-stage" id="trainer">
|
||||
<div class="coral-orbit" />
|
||||
<div class="mint-orbit" />
|
||||
<.board_preview />
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<.mailing_list_signup :if={@mailing_list_enabled} />
|
||||
</main>
|
||||
@@ -70,7 +70,7 @@
|
||||
</div>
|
||||
</section> --%>
|
||||
|
||||
<.mailing_list_signup />
|
||||
<.mailing_list_signup :if={@mailing_list_enabled} />
|
||||
|
||||
<%!-- <section class="progress-banner" id="progress">
|
||||
<div>
|
||||
|
||||
@@ -9,15 +9,25 @@ defmodule ChesstrainerWeb.RobotsController do
|
||||
use ChesstrainerWeb, :controller
|
||||
|
||||
def index(conn, _params) do
|
||||
body = """
|
||||
User-agent: *
|
||||
Allow: /
|
||||
body =
|
||||
if prelaunch?() do
|
||||
"""
|
||||
User-agent: *
|
||||
Disallow: /
|
||||
"""
|
||||
else
|
||||
"""
|
||||
User-agent: *
|
||||
Allow: /
|
||||
|
||||
Sitemap: #{url(conn, ~p"/sitemap.xml")}
|
||||
"""
|
||||
Sitemap: #{url(conn, ~p"/sitemap.xml")}
|
||||
"""
|
||||
end
|
||||
|
||||
conn
|
||||
|> put_resp_content_type("text/plain")
|
||||
|> send_resp(200, body)
|
||||
end
|
||||
|
||||
defp prelaunch?, do: Application.get_env(:chesstrainer, :prelaunch, false)
|
||||
end
|
||||
|
||||
@@ -5,20 +5,28 @@ defmodule ChesstrainerWeb.UserSessionController do
|
||||
alias ChesstrainerWeb.UserAuth
|
||||
|
||||
def create(conn, %{"user" => user_params}) do
|
||||
authenticate(conn, user_params, ~p"/users/log-in", nil)
|
||||
end
|
||||
|
||||
def create_admin(conn, %{"user" => user_params}) do
|
||||
if ChesstrainerWeb.prelaunch?() do
|
||||
authenticate(conn, user_params, ~p"/admin/log-in", ~p"/admin/endgames")
|
||||
else
|
||||
authenticate(conn, user_params, ~p"/users/log-in", nil)
|
||||
end
|
||||
end
|
||||
|
||||
defp authenticate(conn, user_params, failure_path, success_path) do
|
||||
%{"email" => login, "password" => password} = user_params
|
||||
|
||||
case Accounts.get_user_by_login_and_password(login, password) do
|
||||
nil ->
|
||||
# In order to prevent user enumeration attacks, don't disclose whether
|
||||
# the email or username is registered.
|
||||
conn
|
||||
|> put_flash(:error, "Invalid email, username or password")
|
||||
|> put_flash(:email, String.slice(login, 0, 160))
|
||||
|> redirect(to: ~p"/users/log-in")
|
||||
invalid_credentials(conn, login, failure_path)
|
||||
|
||||
user ->
|
||||
if user.confirmed_at do
|
||||
conn
|
||||
|> maybe_put_return_to(success_path)
|
||||
|> put_flash(:info, "Welcome back!")
|
||||
|> UserAuth.log_in_user(user, user_params)
|
||||
else
|
||||
@@ -30,14 +38,21 @@ defmodule ChesstrainerWeb.UserSessionController do
|
||||
|
||||
# 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")
|
||||
invalid_credentials(conn, login, failure_path)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp invalid_credentials(conn, login, path) do
|
||||
conn
|
||||
|> put_flash(:error, "Invalid email, username or password")
|
||||
|> put_flash(:email, String.slice(login, 0, 160))
|
||||
|> redirect(to: path)
|
||||
end
|
||||
|
||||
defp maybe_put_return_to(conn, nil), do: conn
|
||||
defp maybe_put_return_to(conn, path), do: put_session(conn, :user_return_to, path)
|
||||
|
||||
def update_password(conn, %{"user" => user_params}) do
|
||||
user = conn.assigns.current_scope.user
|
||||
true = Accounts.sudo_mode?(user)
|
||||
|
||||
Reference in New Issue
Block a user