mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 11:42:07 +00:00
prelaunch env var prevents the site from being operational
This commit is contained in:
@@ -2,6 +2,9 @@ SECRET_KEY_BASE=...
|
|||||||
DATABASE_URL=...
|
DATABASE_URL=...
|
||||||
PHX_HOST=innersquare.app
|
PHX_HOST=innersquare.app
|
||||||
PORT=4000
|
PORT=4000
|
||||||
|
PRELAUNCH=true
|
||||||
|
MAILING_LIST_ENABLED=false
|
||||||
SES_REGION=eu-north-1
|
SES_REGION=eu-north-1
|
||||||
SES_ACCESS_KEY_ID=AKIA...
|
SES_ACCESS_KEY_ID=AKIA...
|
||||||
SES_SECRET_ACCESS_KEY=...
|
SES_SECRET_ACCESS_KEY=...
|
||||||
|
TABLEBASE_PATH=
|
||||||
|
|||||||
@@ -17,6 +17,11 @@ config :chesstrainer, :branding,
|
|||||||
support_email: "support@innersquare.app",
|
support_email: "support@innersquare.app",
|
||||||
mail_from: "no-reply@innersquare.app"
|
mail_from: "no-reply@innersquare.app"
|
||||||
|
|
||||||
|
# Prelaunch gating. Production sets these from PRELAUNCH and
|
||||||
|
# MAILING_LIST_ENABLED; dev and test use these defaults.
|
||||||
|
config :chesstrainer, :prelaunch, false
|
||||||
|
config :chesstrainer, :mailing_list_enabled, true
|
||||||
|
|
||||||
config :chesstrainer, :scopes,
|
config :chesstrainer, :scopes,
|
||||||
user: [
|
user: [
|
||||||
default: true,
|
default: true,
|
||||||
|
|||||||
+5
-1
@@ -15,11 +15,15 @@ config :chesstrainer, ChesstrainerWeb.Endpoint,
|
|||||||
force_ssl: [
|
force_ssl: [
|
||||||
rewrite_on: [:x_forwarded_proto],
|
rewrite_on: [:x_forwarded_proto],
|
||||||
exclude: [
|
exclude: [
|
||||||
# paths: ["/health"],
|
paths: ["/health"],
|
||||||
hosts: ["localhost", "127.0.0.1"]
|
hosts: ["localhost", "127.0.0.1"]
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
|
|
||||||
|
# Fail closed in production: the mailing list stays off until SES production
|
||||||
|
# access is granted and MAILING_LIST_ENABLED is set explicitly.
|
||||||
|
config :chesstrainer, :mailing_list_enabled, false
|
||||||
|
|
||||||
# Configure Swoosh API Client
|
# Configure Swoosh API Client
|
||||||
config :swoosh, api_client: Swoosh.ApiClient.Req
|
config :swoosh, api_client: Swoosh.ApiClient.Req
|
||||||
|
|
||||||
|
|||||||
@@ -20,6 +20,14 @@ if System.get_env("PHX_SERVER") do
|
|||||||
config :chesstrainer, ChesstrainerWeb.Endpoint, server: true
|
config :chesstrainer, ChesstrainerWeb.Endpoint, server: true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if prelaunch = System.get_env("PRELAUNCH") do
|
||||||
|
config :chesstrainer, :prelaunch, prelaunch in ~w(true 1)
|
||||||
|
end
|
||||||
|
|
||||||
|
if mailing_list_enabled = System.get_env("MAILING_LIST_ENABLED") do
|
||||||
|
config :chesstrainer, :mailing_list_enabled, mailing_list_enabled in ~w(true 1)
|
||||||
|
end
|
||||||
|
|
||||||
# Local Syzygy tablebases: set TABLEBASE_PATH to the directory holding the
|
# Local Syzygy tablebases: set TABLEBASE_PATH to the directory holding the
|
||||||
# .rtbw/.rtbz files. When unset, the local backend stays disabled and lookups
|
# .rtbw/.rtbz files. When unset, the local backend stays disabled and lookups
|
||||||
# fall back to Lichess.
|
# fall back to Lichess.
|
||||||
|
|||||||
@@ -19,6 +19,14 @@ defmodule ChesstrainerWeb do
|
|||||||
|
|
||||||
def static_paths, do: ~w(assets fonts images favicon.ico)
|
def static_paths, do: ~w(assets fonts images favicon.ico)
|
||||||
|
|
||||||
|
@doc "Whether the app is gated behind the prelaunch page."
|
||||||
|
def prelaunch?, do: Application.get_env(:chesstrainer, :prelaunch, false)
|
||||||
|
|
||||||
|
@doc "Whether the mailing-list form and endpoint are open."
|
||||||
|
def mailing_list_enabled? do
|
||||||
|
Application.get_env(:chesstrainer, :mailing_list_enabled, true)
|
||||||
|
end
|
||||||
|
|
||||||
def router do
|
def router do
|
||||||
quote do
|
quote do
|
||||||
use Phoenix.Router, helpers: false
|
use Phoenix.Router, helpers: false
|
||||||
|
|||||||
@@ -63,11 +63,17 @@ defmodule ChesstrainerWeb.Layouts do
|
|||||||
doc: "the current [scope](https://phoenix.hexdocs.pm/scopes.html)"
|
doc: "the current [scope](https://phoenix.hexdocs.pm/scopes.html)"
|
||||||
|
|
||||||
def navbar(assigns) do
|
def navbar(assigns) do
|
||||||
|
user = signed_in_user(assigns.current_scope)
|
||||||
|
nav_links = nav_links(assigns.current_scope)
|
||||||
|
prelaunch = ChesstrainerWeb.prelaunch?()
|
||||||
|
|
||||||
assigns =
|
assigns =
|
||||||
assigns
|
assigns
|
||||||
|> assign(:nav_links, nav_links(assigns.current_scope))
|
|> assign(:nav_links, nav_links)
|
||||||
|> assign(:start_href, start_href(assigns.current_scope))
|
|> assign(:start_href, start_href(assigns.current_scope))
|
||||||
|> assign(:user, signed_in_user(assigns.current_scope))
|
|> assign(:user, user)
|
||||||
|
|> assign(:prelaunch, prelaunch)
|
||||||
|
|> assign(:show_nav, not prelaunch or not is_nil(user) or nav_links != [])
|
||||||
|
|
||||||
~H"""
|
~H"""
|
||||||
<header>
|
<header>
|
||||||
@@ -80,6 +86,7 @@ defmodule ChesstrainerWeb.Layouts do
|
|||||||
<div class="nav-right">
|
<div class="nav-right">
|
||||||
<.colour_mode_toggle />
|
<.colour_mode_toggle />
|
||||||
|
|
||||||
|
<%= if @show_nav do %>
|
||||||
<button
|
<button
|
||||||
class="menu-button"
|
class="menu-button"
|
||||||
type="button"
|
type="button"
|
||||||
@@ -107,14 +114,19 @@ defmodule ChesstrainerWeb.Layouts do
|
|||||||
</div>
|
</div>
|
||||||
</details>
|
</details>
|
||||||
<% else %>
|
<% else %>
|
||||||
|
<%= if !@prelaunch do %>
|
||||||
<.link href={~p"/users/log-in"}>Log in</.link>
|
<.link href={~p"/users/log-in"}>Log in</.link>
|
||||||
<.link href={~p"/users/register"}>Register</.link>
|
<.link href={~p"/users/register"}>Register</.link>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
<% end %>
|
||||||
|
|
||||||
|
<%= if !@prelaunch do %>
|
||||||
<.pill_button variant={:nav} href={@start_href}>
|
<.pill_button variant={:nav} href={@start_href}>
|
||||||
Start training <.icon_arrow />
|
Start training <.icon_arrow />
|
||||||
</.pill_button>
|
</.pill_button>
|
||||||
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
</nav>
|
</nav>
|
||||||
</header>
|
</header>
|
||||||
|
|||||||
@@ -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
|
@token_max_age 7 * 24 * 60 * 60
|
||||||
|
|
||||||
def create(conn, %{"subscriber" => params}) when is_map(params) do
|
def create(conn, %{"subscriber" => params}) when is_map(params) do
|
||||||
email = params["email"]
|
if mailing_list_enabled?() do
|
||||||
|
create_subscription(conn, params)
|
||||||
cond do
|
else
|
||||||
honeypot_filled?(params) ->
|
closed(conn)
|
||||||
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
|
||||||
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
|
def confirm(conn, %{"token" => token}) do
|
||||||
case Phoenix.Token.verify(
|
case Phoenix.Token.verify(
|
||||||
@@ -63,6 +55,24 @@ defmodule ChesstrainerWeb.MailingListController do
|
|||||||
end
|
end
|
||||||
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
|
defp subscribe(conn, email) do
|
||||||
email = String.trim(email)
|
email = String.trim(email)
|
||||||
|
|
||||||
@@ -122,4 +132,14 @@ defmodule ChesstrainerWeb.MailingListController do
|
|||||||
|> put_flash(:error, "Enter a valid email address.")
|
|> put_flash(:error, "Enter a valid email address.")
|
||||||
|> redirect(to: @return_to)
|
|> redirect(to: @return_to)
|
||||||
end
|
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
|
end
|
||||||
|
|||||||
@@ -11,6 +11,9 @@ defmodule ChesstrainerWeb.PageController do
|
|||||||
"Learn to see the winning move and make the endgame your strongest phase."
|
"Learn to see the winning move and make the endgame your strongest phase."
|
||||||
|
|
||||||
def home(conn, _params) do
|
def home(conn, _params) do
|
||||||
|
if prelaunch?() do
|
||||||
|
render_coming_soon(conn)
|
||||||
|
else
|
||||||
endgames = Endgames.curated_endgames()
|
endgames = Endgames.curated_endgames()
|
||||||
|
|
||||||
conn
|
conn
|
||||||
@@ -19,6 +22,7 @@ defmodule ChesstrainerWeb.PageController do
|
|||||||
|> assign(:title_suffix, "")
|
|> assign(:title_suffix, "")
|
||||||
|> assign(:meta_description, @description)
|
|> assign(:meta_description, @description)
|
||||||
|> assign(:canonical_url, current_url(conn))
|
|> assign(:canonical_url, current_url(conn))
|
||||||
|
|> assign(:mailing_list_enabled, mailing_list_enabled?())
|
||||||
|> render(:home,
|
|> render(:home,
|
||||||
endgames: endgames,
|
endgames: endgames,
|
||||||
active_endgame: DailyPuzzles.today(),
|
active_endgame: DailyPuzzles.today(),
|
||||||
@@ -28,6 +32,7 @@ defmodule ChesstrainerWeb.PageController do
|
|||||||
today: Date.utc_today()
|
today: Date.utc_today()
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def privacy(conn, _params) do
|
def privacy(conn, _params) do
|
||||||
conn
|
conn
|
||||||
@@ -61,4 +66,22 @@ defmodule ChesstrainerWeb.PageController do
|
|||||||
|> assign(:noindex, true)
|
|> assign(:noindex, true)
|
||||||
|> render(:not_found)
|
|> render(:not_found)
|
||||||
end
|
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
|
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>
|
</div>
|
||||||
</section> --%>
|
</section> --%>
|
||||||
|
|
||||||
<.mailing_list_signup />
|
<.mailing_list_signup :if={@mailing_list_enabled} />
|
||||||
|
|
||||||
<%!-- <section class="progress-banner" id="progress">
|
<%!-- <section class="progress-banner" id="progress">
|
||||||
<div>
|
<div>
|
||||||
|
|||||||
@@ -9,15 +9,25 @@ defmodule ChesstrainerWeb.RobotsController do
|
|||||||
use ChesstrainerWeb, :controller
|
use ChesstrainerWeb, :controller
|
||||||
|
|
||||||
def index(conn, _params) do
|
def index(conn, _params) do
|
||||||
body = """
|
body =
|
||||||
|
if prelaunch?() do
|
||||||
|
"""
|
||||||
|
User-agent: *
|
||||||
|
Disallow: /
|
||||||
|
"""
|
||||||
|
else
|
||||||
|
"""
|
||||||
User-agent: *
|
User-agent: *
|
||||||
Allow: /
|
Allow: /
|
||||||
|
|
||||||
Sitemap: #{url(conn, ~p"/sitemap.xml")}
|
Sitemap: #{url(conn, ~p"/sitemap.xml")}
|
||||||
"""
|
"""
|
||||||
|
end
|
||||||
|
|
||||||
conn
|
conn
|
||||||
|> put_resp_content_type("text/plain")
|
|> put_resp_content_type("text/plain")
|
||||||
|> send_resp(200, body)
|
|> send_resp(200, body)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp prelaunch?, do: Application.get_env(:chesstrainer, :prelaunch, false)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -5,20 +5,28 @@ defmodule ChesstrainerWeb.UserSessionController do
|
|||||||
alias ChesstrainerWeb.UserAuth
|
alias ChesstrainerWeb.UserAuth
|
||||||
|
|
||||||
def create(conn, %{"user" => user_params}) do
|
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
|
%{"email" => login, "password" => password} = user_params
|
||||||
|
|
||||||
case Accounts.get_user_by_login_and_password(login, password) do
|
case Accounts.get_user_by_login_and_password(login, password) do
|
||||||
nil ->
|
nil ->
|
||||||
# In order to prevent user enumeration attacks, don't disclose whether
|
invalid_credentials(conn, login, failure_path)
|
||||||
# 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")
|
|
||||||
|
|
||||||
user ->
|
user ->
|
||||||
if user.confirmed_at do
|
if user.confirmed_at do
|
||||||
conn
|
conn
|
||||||
|
|> maybe_put_return_to(success_path)
|
||||||
|> put_flash(:info, "Welcome back!")
|
|> put_flash(:info, "Welcome back!")
|
||||||
|> UserAuth.log_in_user(user, user_params)
|
|> UserAuth.log_in_user(user, user_params)
|
||||||
else
|
else
|
||||||
@@ -30,14 +38,21 @@ defmodule ChesstrainerWeb.UserSessionController do
|
|||||||
|
|
||||||
# In order to prevent user enumeration attacks, report the same error
|
# In order to prevent user enumeration attacks, report the same error
|
||||||
# as invalid credentials.
|
# as invalid credentials.
|
||||||
|
invalid_credentials(conn, login, failure_path)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp invalid_credentials(conn, login, path) do
|
||||||
conn
|
conn
|
||||||
|> put_flash(:error, "Invalid email, username or password")
|
|> put_flash(:error, "Invalid email, username or password")
|
||||||
|> put_flash(:email, String.slice(login, 0, 160))
|
|> put_flash(:email, String.slice(login, 0, 160))
|
||||||
|> redirect(to: ~p"/users/log-in")
|
|> redirect(to: path)
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
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
|
def update_password(conn, %{"user" => user_params}) do
|
||||||
user = conn.assigns.current_scope.user
|
user = conn.assigns.current_scope.user
|
||||||
true = Accounts.sudo_mode?(user)
|
true = Accounts.sudo_mode?(user)
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ defmodule ChesstrainerWeb.UserLive.Login do
|
|||||||
<%= if @current_scope do %>
|
<%= if @current_scope do %>
|
||||||
You need to reauthenticate to perform sensitive actions on your account.
|
You need to reauthenticate to perform sensitive actions on your account.
|
||||||
<% else %>
|
<% else %>
|
||||||
<p class="settings-subtitle my-4">
|
<p :if={!@prelaunch} class="settings-subtitle my-4">
|
||||||
Don't have an account? <.link
|
Don't have an account? <.link
|
||||||
navigate={~p"/users/register"}
|
navigate={~p"/users/register"}
|
||||||
class="font-semibold text-primary-text hover:underline"
|
class="font-semibold text-primary-text hover:underline"
|
||||||
@@ -25,7 +25,7 @@ defmodule ChesstrainerWeb.UserLive.Login do
|
|||||||
:let={f}
|
:let={f}
|
||||||
for={@form}
|
for={@form}
|
||||||
id="login_form_password"
|
id="login_form_password"
|
||||||
action={~p"/users/log-in"}
|
action={@login_action}
|
||||||
phx-submit="submit_password"
|
phx-submit="submit_password"
|
||||||
phx-trigger-action={@trigger_submit}
|
phx-trigger-action={@trigger_submit}
|
||||||
>
|
>
|
||||||
@@ -73,8 +73,18 @@ defmodule ChesstrainerWeb.UserLive.Login do
|
|||||||
get_in(socket.assigns, [:current_scope, Access.key(:user), Access.key(:email)])
|
get_in(socket.assigns, [:current_scope, Access.key(:user), Access.key(:email)])
|
||||||
|
|
||||||
form = to_form(%{"email" => email}, as: "user")
|
form = to_form(%{"email" => email}, as: "user")
|
||||||
|
prelaunch = ChesstrainerWeb.prelaunch?()
|
||||||
|
|
||||||
{:ok, assign(socket, form: form, trigger_submit: false)}
|
login_action =
|
||||||
|
if prelaunch, do: ~p"/admin/log-in", else: ~p"/users/log-in"
|
||||||
|
|
||||||
|
{:ok,
|
||||||
|
assign(socket,
|
||||||
|
form: form,
|
||||||
|
trigger_submit: false,
|
||||||
|
prelaunch: prelaunch,
|
||||||
|
login_action: login_action
|
||||||
|
)}
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
|
|||||||
@@ -74,19 +74,40 @@ defmodule ChesstrainerWeb.UserLive.Registration do
|
|||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def mount(_params, _session, %{assigns: %{current_scope: %{user: user}}} = socket)
|
def mount(_params, _session, socket) do
|
||||||
|
if prelaunch?() do
|
||||||
|
{:ok, redirect(socket, to: ~p"/")}
|
||||||
|
else
|
||||||
|
do_mount(socket)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def handle_event("save", %{"user" => user_params}, socket) do
|
||||||
|
if prelaunch?() do
|
||||||
|
{:noreply, push_navigate(socket, to: ~p"/")}
|
||||||
|
else
|
||||||
|
save_registration(socket, user_params)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_event("validate", %{"user" => user_params}, socket) do
|
||||||
|
changeset = Accounts.change_user_registration(%User{}, user_params, validate_unique: false)
|
||||||
|
{:noreply, assign_form(socket, Map.put(changeset, :action, :validate))}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp do_mount(%{assigns: %{current_scope: %{user: user}}} = socket)
|
||||||
when not is_nil(user) do
|
when not is_nil(user) do
|
||||||
{:ok, redirect(socket, to: ChesstrainerWeb.UserAuth.signed_in_path(socket))}
|
{:ok, redirect(socket, to: ChesstrainerWeb.UserAuth.signed_in_path(socket))}
|
||||||
end
|
end
|
||||||
|
|
||||||
def mount(_params, _session, socket) do
|
defp do_mount(socket) do
|
||||||
changeset = Accounts.change_user_registration(%User{}, %{}, validate_unique: false)
|
changeset = Accounts.change_user_registration(%User{}, %{}, validate_unique: false)
|
||||||
|
|
||||||
{:ok, assign_form(socket, changeset), temporary_assigns: [form: nil]}
|
{:ok, assign_form(socket, changeset), temporary_assigns: [form: nil]}
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
defp save_registration(socket, user_params) do
|
||||||
def handle_event("save", %{"user" => user_params}, socket) do
|
|
||||||
case Accounts.register_user(user_params) do
|
case Accounts.register_user(user_params) do
|
||||||
{:ok, user} ->
|
{:ok, user} ->
|
||||||
if user.mailing_list, do: MailingList.subscribe_user(user)
|
if user.mailing_list, do: MailingList.subscribe_user(user)
|
||||||
@@ -110,10 +131,7 @@ defmodule ChesstrainerWeb.UserLive.Registration do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle_event("validate", %{"user" => user_params}, socket) do
|
defp prelaunch?, do: Application.get_env(:chesstrainer, :prelaunch, false)
|
||||||
changeset = Accounts.change_user_registration(%User{}, user_params, validate_unique: false)
|
|
||||||
{:noreply, assign_form(socket, Map.put(changeset, :action, :validate))}
|
|
||||||
end
|
|
||||||
|
|
||||||
defp assign_form(socket, %Ecto.Changeset{} = changeset) do
|
defp assign_form(socket, %Ecto.Changeset{} = changeset) do
|
||||||
form = to_form(changeset, as: "user")
|
form = to_form(changeset, as: "user")
|
||||||
|
|||||||
@@ -0,0 +1,57 @@
|
|||||||
|
defmodule ChesstrainerWeb.Plugs.Prelaunch do
|
||||||
|
@moduledoc """
|
||||||
|
Redirects every non-public route to the home page while the app is gated.
|
||||||
|
|
||||||
|
The allowlist is deliberately small: legal pages, the mailing list, auth
|
||||||
|
entry points, admin, crawler files, health, and the static/LiveView plumbing.
|
||||||
|
Everything else redirects to `/` with `X-Robots-Tag: noindex, nofollow`.
|
||||||
|
|
||||||
|
Set `PRELAUNCH=true` to enable the gate.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import Plug.Conn
|
||||||
|
|
||||||
|
@allowed_exact ~w(
|
||||||
|
/
|
||||||
|
/privacy
|
||||||
|
/terms-conditions
|
||||||
|
/health
|
||||||
|
/robots.txt
|
||||||
|
/sitemap.xml
|
||||||
|
/favicon.ico
|
||||||
|
)
|
||||||
|
|
||||||
|
@allowed_prefixes ~w(
|
||||||
|
/mailing-list
|
||||||
|
/users/log-out
|
||||||
|
/users/confirm
|
||||||
|
/users/reset-password
|
||||||
|
/users/settings
|
||||||
|
/admin
|
||||||
|
/live
|
||||||
|
/assets
|
||||||
|
/fonts
|
||||||
|
/images
|
||||||
|
/dev
|
||||||
|
)
|
||||||
|
|
||||||
|
def init(opts), do: opts
|
||||||
|
|
||||||
|
def call(conn, _opts) do
|
||||||
|
if enabled?() and not allowed?(conn.request_path) do
|
||||||
|
conn
|
||||||
|
|> put_resp_header("x-robots-tag", "noindex, nofollow")
|
||||||
|
|> Phoenix.Controller.redirect(to: "/")
|
||||||
|
|> halt()
|
||||||
|
else
|
||||||
|
conn
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp enabled?, do: Application.get_env(:chesstrainer, :prelaunch, false)
|
||||||
|
|
||||||
|
defp allowed?(path) do
|
||||||
|
path in @allowed_exact or
|
||||||
|
Enum.any?(@allowed_prefixes, &String.starts_with?(path, &1))
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -10,6 +10,7 @@ defmodule ChesstrainerWeb.Router do
|
|||||||
plug :put_root_layout, html: {ChesstrainerWeb.Layouts, :root}
|
plug :put_root_layout, html: {ChesstrainerWeb.Layouts, :root}
|
||||||
plug :protect_from_forgery
|
plug :protect_from_forgery
|
||||||
plug :put_secure_browser_headers
|
plug :put_secure_browser_headers
|
||||||
|
plug ChesstrainerWeb.Plugs.Prelaunch
|
||||||
plug :fetch_current_scope_for_user
|
plug :fetch_current_scope_for_user
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -34,13 +35,17 @@ defmodule ChesstrainerWeb.Router do
|
|||||||
get "/terms-conditions", PageController, :terms_conditions
|
get "/terms-conditions", PageController, :terms_conditions
|
||||||
post "/mailing-list", MailingListController, :create
|
post "/mailing-list", MailingListController, :create
|
||||||
get "/mailing-list/confirm/:token", MailingListController, :confirm
|
get "/mailing-list/confirm/:token", MailingListController, :confirm
|
||||||
|
post "/admin/log-in", UserSessionController, :create_admin
|
||||||
end
|
end
|
||||||
|
|
||||||
scope "/admin", ChesstrainerWeb do
|
scope "/admin", ChesstrainerWeb do
|
||||||
pipe_through [:browser, :noindex, :require_admin_user]
|
pipe_through [:browser, :noindex, :require_admin_user]
|
||||||
|
|
||||||
live_session :endgames,
|
live_session :endgames,
|
||||||
on_mount: [{ChesstrainerWeb.UserAuth, :require_admin}] do
|
on_mount: [
|
||||||
|
{ChesstrainerWeb.UserAuth, :enforce_prelaunch},
|
||||||
|
{ChesstrainerWeb.UserAuth, :require_admin}
|
||||||
|
] do
|
||||||
live "/endgames", EndgameLive.Index, :index
|
live "/endgames", EndgameLive.Index, :index
|
||||||
live "/endgames/new", EndgameLive.Form, :new
|
live "/endgames/new", EndgameLive.Form, :new
|
||||||
live "/endgames/:id", EndgameLive.Show, :show
|
live "/endgames/:id", EndgameLive.Show, :show
|
||||||
@@ -76,7 +81,10 @@ defmodule ChesstrainerWeb.Router do
|
|||||||
pipe_through [:browser, :require_authenticated_user]
|
pipe_through [:browser, :require_authenticated_user]
|
||||||
|
|
||||||
live_session :require_authenticated_user,
|
live_session :require_authenticated_user,
|
||||||
on_mount: [{ChesstrainerWeb.UserAuth, :require_authenticated}] do
|
on_mount: [
|
||||||
|
{ChesstrainerWeb.UserAuth, :enforce_prelaunch},
|
||||||
|
{ChesstrainerWeb.UserAuth, :require_authenticated}
|
||||||
|
] do
|
||||||
live "/endgames", EndgameLive.PlayerIndex, :index
|
live "/endgames", EndgameLive.PlayerIndex, :index
|
||||||
live "/train", EndgameLive.Train, :train
|
live "/train", EndgameLive.Train, :train
|
||||||
live "/users/profile", UserLive.Profile, :account
|
live "/users/profile", UserLive.Profile, :account
|
||||||
@@ -95,11 +103,15 @@ defmodule ChesstrainerWeb.Router do
|
|||||||
pipe_through [:browser]
|
pipe_through [:browser]
|
||||||
|
|
||||||
live_session :current_user,
|
live_session :current_user,
|
||||||
on_mount: [{ChesstrainerWeb.UserAuth, :mount_current_scope}] do
|
on_mount: [
|
||||||
|
{ChesstrainerWeb.UserAuth, :enforce_prelaunch},
|
||||||
|
{ChesstrainerWeb.UserAuth, :mount_current_scope}
|
||||||
|
] do
|
||||||
live "/guest-train", EndgameLive.GuestTrain, :play
|
live "/guest-train", EndgameLive.GuestTrain, :play
|
||||||
live "/endgames/:public_code/play", EndgameLive.Play, :play
|
live "/endgames/:public_code/play", EndgameLive.Play, :play
|
||||||
live "/users/register", UserLive.Registration, :new
|
live "/users/register", UserLive.Registration, :new
|
||||||
live "/users/log-in", UserLive.Login, :new
|
live "/users/log-in", UserLive.Login, :new
|
||||||
|
live "/admin/log-in", UserLive.Login, :new
|
||||||
live "/users/reset-password", UserLive.ResetPassword, :new
|
live "/users/reset-password", UserLive.ResetPassword, :new
|
||||||
live "/users/reset-password/:token", UserLive.ResetPassword, :edit
|
live "/users/reset-password/:token", UserLive.ResetPassword, :edit
|
||||||
end
|
end
|
||||||
@@ -115,6 +127,7 @@ defmodule ChesstrainerWeb.Router do
|
|||||||
scope "/", ChesstrainerWeb do
|
scope "/", ChesstrainerWeb do
|
||||||
pipe_through :seo
|
pipe_through :seo
|
||||||
|
|
||||||
|
get "/health", HealthController, :index
|
||||||
get "/robots.txt", RobotsController, :index
|
get "/robots.txt", RobotsController, :index
|
||||||
get "/sitemap.xml", SitemapController, :index
|
get "/sitemap.xml", SitemapController, :index
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -212,6 +212,24 @@ defmodule ChesstrainerWeb.UserAuth do
|
|||||||
live "/profile", ProfileLive, :index
|
live "/profile", ProfileLive, :index
|
||||||
end
|
end
|
||||||
"""
|
"""
|
||||||
|
@prelaunch_allowed_views [
|
||||||
|
ChesstrainerWeb.UserLive.Login,
|
||||||
|
ChesstrainerWeb.UserLive.ResetPassword,
|
||||||
|
ChesstrainerWeb.UserLive.Profile,
|
||||||
|
ChesstrainerWeb.EndgameLive.Index,
|
||||||
|
ChesstrainerWeb.EndgameLive.Form,
|
||||||
|
ChesstrainerWeb.EndgameLive.Show
|
||||||
|
]
|
||||||
|
|
||||||
|
def on_mount(:enforce_prelaunch, _params, _session, socket) do
|
||||||
|
if Application.get_env(:chesstrainer, :prelaunch, false) and
|
||||||
|
socket.view not in @prelaunch_allowed_views do
|
||||||
|
{:halt, Phoenix.LiveView.redirect(socket, to: ~p"/")}
|
||||||
|
else
|
||||||
|
{:cont, socket}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def on_mount(:mount_current_scope, _params, session, socket) do
|
def on_mount(:mount_current_scope, _params, session, socket) do
|
||||||
{:cont, mount_current_scope(socket, session)}
|
{:cont, mount_current_scope(socket, session)}
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
[tools]
|
||||||
|
erlang = "28.5"
|
||||||
|
elixir = "1.19.5-otp-28"
|
||||||
|
rust = "1.98.1"
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
defmodule ChesstrainerWeb.MailingListControllerTest do
|
defmodule ChesstrainerWeb.MailingListControllerTest do
|
||||||
use ChesstrainerWeb.ConnCase, async: true
|
use ChesstrainerWeb.ConnCase, async: false
|
||||||
|
|
||||||
alias Chesstrainer.MailingList
|
alias Chesstrainer.MailingList
|
||||||
alias Chesstrainer.MailingList.Subscriber
|
alias Chesstrainer.MailingList.Subscriber
|
||||||
@@ -169,6 +169,22 @@ defmodule ChesstrainerWeb.MailingListControllerTest do
|
|||||||
assert redirected_to(conn) == "/#mailing-list"
|
assert redirected_to(conn) == "/#mailing-list"
|
||||||
assert Phoenix.Flash.get(conn.assigns.flash, :error) =~ "valid email address"
|
assert Phoenix.Flash.get(conn.assigns.flash, :error) =~ "valid email address"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "rejects signups when the mailing list is disabled", %{conn: conn} do
|
||||||
|
previous = Application.get_env(:chesstrainer, :mailing_list_enabled)
|
||||||
|
on_exit(fn -> restore_env(:mailing_list_enabled, previous) end)
|
||||||
|
Application.put_env(:chesstrainer, :mailing_list_enabled, false)
|
||||||
|
|
||||||
|
conn =
|
||||||
|
post(conn, ~p"/mailing-list",
|
||||||
|
subscriber: %{email: "player@example.com", form_token: form_token()}
|
||||||
|
)
|
||||||
|
|
||||||
|
assert redirected_to(conn) == "/#mailing-list"
|
||||||
|
assert Phoenix.Flash.get(conn.assigns.flash, :info) =~ "isn't open yet"
|
||||||
|
refute_received {:email, _email}
|
||||||
|
assert Repo.aggregate(Subscriber, :count) == 0
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "GET /mailing-list/confirm/:token" do
|
describe "GET /mailing-list/confirm/:token" do
|
||||||
@@ -227,4 +243,7 @@ defmodule ChesstrainerWeb.MailingListControllerTest do
|
|||||||
defp form_token do
|
defp form_token do
|
||||||
MailingListFormToken.sign(System.system_time(:second) - 5)
|
MailingListFormToken.sign(System.system_time(:second) - 5)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp restore_env(key, nil), do: Application.delete_env(:chesstrainer, key)
|
||||||
|
defp restore_env(key, value), do: Application.put_env(:chesstrainer, key, value)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -180,6 +180,29 @@ defmodule ChesstrainerWeb.PageControllerTest do
|
|||||||
assert LazyHTML.text(LazyHTML.query(document, ".rating-note strong")) == "0"
|
assert LazyHTML.text(LazyHTML.query(document, ".rating-note strong")) == "0"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "renders the coming soon page while prelaunch is on", %{conn: conn} do
|
||||||
|
previous = Application.get_env(:chesstrainer, :prelaunch)
|
||||||
|
on_exit(fn -> restore_env(:prelaunch, previous) end)
|
||||||
|
Application.put_env(:chesstrainer, :prelaunch, true)
|
||||||
|
|
||||||
|
html = conn |> get(~p"/") |> html_response(200)
|
||||||
|
|
||||||
|
assert html =~ "coming soon"
|
||||||
|
assert html =~ "Master the"
|
||||||
|
refute html =~ "Try today"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "hides the mailing list when it is disabled", %{conn: conn} do
|
||||||
|
previous = Application.get_env(:chesstrainer, :mailing_list_enabled)
|
||||||
|
on_exit(fn -> restore_env(:mailing_list_enabled, previous) end)
|
||||||
|
Application.put_env(:chesstrainer, :mailing_list_enabled, false)
|
||||||
|
|
||||||
|
html = conn |> get(~p"/") |> html_response(200)
|
||||||
|
|
||||||
|
refute html =~ ~s(id="mailing-list-form")
|
||||||
|
refute html =~ "Join the list"
|
||||||
|
end
|
||||||
|
|
||||||
test "renders the mailing list signup form", %{conn: conn} do
|
test "renders the mailing list signup form", %{conn: conn} do
|
||||||
html = conn |> get(~p"/") |> html_response(200)
|
html = conn |> get(~p"/") |> html_response(200)
|
||||||
document = LazyHTML.from_document(html)
|
document = LazyHTML.from_document(html)
|
||||||
@@ -305,4 +328,7 @@ defmodule ChesstrainerWeb.PageControllerTest do
|
|||||||
assert html =~ "Browse puzzles"
|
assert html =~ "Browse puzzles"
|
||||||
assert html =~ ~s(href="/endgames")
|
assert html =~ ~s(href="/endgames")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp restore_env(key, nil), do: Application.delete_env(:chesstrainer, key)
|
||||||
|
defp restore_env(key, value), do: Application.put_env(:chesstrainer, key, value)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
defmodule ChesstrainerWeb.RobotsControllerTest do
|
defmodule ChesstrainerWeb.RobotsControllerTest do
|
||||||
use ChesstrainerWeb.ConnCase, async: true
|
use ChesstrainerWeb.ConnCase, async: false
|
||||||
|
|
||||||
alias ChesstrainerWeb.Endpoint
|
alias ChesstrainerWeb.Endpoint
|
||||||
|
|
||||||
@@ -18,5 +18,20 @@ defmodule ChesstrainerWeb.RobotsControllerTest do
|
|||||||
assert [content_type] = get_resp_header(conn, "content-type")
|
assert [content_type] = get_resp_header(conn, "content-type")
|
||||||
assert content_type =~ "text/plain"
|
assert content_type =~ "text/plain"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "disallows crawlers while prelaunch is on", %{conn: conn} do
|
||||||
|
previous = Application.get_env(:chesstrainer, :prelaunch)
|
||||||
|
on_exit(fn -> restore_env(:prelaunch, previous) end)
|
||||||
|
Application.put_env(:chesstrainer, :prelaunch, true)
|
||||||
|
|
||||||
|
body = conn |> get(~p"/robots.txt") |> response(200)
|
||||||
|
|
||||||
|
assert body =~ "User-agent: *"
|
||||||
|
assert body =~ "Disallow: /"
|
||||||
|
refute body =~ "Allow: /"
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp restore_env(key, nil), do: Application.delete_env(:chesstrainer, key)
|
||||||
|
defp restore_env(key, value), do: Application.put_env(:chesstrainer, key, value)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -0,0 +1,85 @@
|
|||||||
|
defmodule ChesstrainerWeb.Plugs.PrelaunchTest do
|
||||||
|
use ChesstrainerWeb.ConnCase, async: false
|
||||||
|
|
||||||
|
import Chesstrainer.AccountsFixtures
|
||||||
|
import Phoenix.LiveViewTest
|
||||||
|
|
||||||
|
alias Chesstrainer.Accounts.User
|
||||||
|
alias Chesstrainer.Repo
|
||||||
|
|
||||||
|
setup do
|
||||||
|
previous = Application.get_env(:chesstrainer, :prelaunch)
|
||||||
|
on_exit(fn -> restore_env(:prelaunch, previous) end)
|
||||||
|
Application.put_env(:chesstrainer, :prelaunch, true)
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
|
||||||
|
test "allows the public pages, legal pages, health, and admin login", %{conn: conn} do
|
||||||
|
assert conn |> get(~p"/") |> html_response(200)
|
||||||
|
assert conn |> get(~p"/privacy") |> html_response(200)
|
||||||
|
assert conn |> get(~p"/terms-conditions") |> html_response(200)
|
||||||
|
assert conn |> get(~p"/health") |> response(200)
|
||||||
|
assert conn |> get(~p"/admin/log-in") |> html_response(200)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "hides public auth links and start training", %{conn: conn} do
|
||||||
|
html = conn |> get(~p"/") |> html_response(200)
|
||||||
|
|
||||||
|
refute html =~ ">Log in<"
|
||||||
|
refute html =~ ">Register<"
|
||||||
|
refute html =~ "Start training"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "the hidden admin login uses the admin form action", %{conn: conn} do
|
||||||
|
html = conn |> get(~p"/admin/log-in") |> html_response(200)
|
||||||
|
|
||||||
|
assert html =~ ~s(action="/admin/log-in")
|
||||||
|
refute html =~ "Sign up"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "pauses the public login route", %{conn: conn} do
|
||||||
|
assert conn |> get(~p"/users/log-in") |> redirected_to() == "/"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "admin login redirects to the admin area", %{conn: conn} do
|
||||||
|
user =
|
||||||
|
user_fixture()
|
||||||
|
|> Ecto.Changeset.change(role: :admin)
|
||||||
|
|> Repo.update!()
|
||||||
|
|
||||||
|
conn =
|
||||||
|
post(conn, ~p"/admin/log-in",
|
||||||
|
user: %{email: user.email, password: "hello world!", remember_me: "true"}
|
||||||
|
)
|
||||||
|
|
||||||
|
assert redirected_to(conn) == "/admin/endgames"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "redirects blocked routes to the home page", %{conn: conn} do
|
||||||
|
conn = get(conn, ~p"/train")
|
||||||
|
|
||||||
|
assert redirected_to(conn) == "/"
|
||||||
|
assert get_resp_header(conn, "x-robots-tag") == ["noindex, nofollow"]
|
||||||
|
end
|
||||||
|
|
||||||
|
test "redirects registration and unknown pages", %{conn: conn} do
|
||||||
|
assert conn |> get(~p"/users/register") |> redirected_to() == "/"
|
||||||
|
assert conn |> get("/definitely-not-a-page") |> redirected_to() == "/"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "blocks direct registration LiveView requests", %{conn: conn} do
|
||||||
|
assert {:error, {:redirect, %{to: "/"}}} = live(conn, ~p"/users/register")
|
||||||
|
end
|
||||||
|
|
||||||
|
test "blocks registration reached through LiveView navigation", %{conn: conn} do
|
||||||
|
{:ok, view, _html} = live(conn, ~p"/admin/log-in")
|
||||||
|
|
||||||
|
assert {:error, {:redirect, %{to: "/"}}} =
|
||||||
|
live_redirect(view, to: ~p"/users/register")
|
||||||
|
|
||||||
|
assert Repo.aggregate(User, :count) == 0
|
||||||
|
end
|
||||||
|
|
||||||
|
defp restore_env(key, nil), do: Application.delete_env(:chesstrainer, key)
|
||||||
|
defp restore_env(key, value), do: Application.put_env(:chesstrainer, key, value)
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user