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:
@@ -19,6 +19,14 @@ defmodule ChesstrainerWeb do
|
||||
|
||||
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
|
||||
quote do
|
||||
use Phoenix.Router, helpers: false
|
||||
|
||||
@@ -63,11 +63,17 @@ defmodule ChesstrainerWeb.Layouts do
|
||||
doc: "the current [scope](https://phoenix.hexdocs.pm/scopes.html)"
|
||||
|
||||
def navbar(assigns) do
|
||||
user = signed_in_user(assigns.current_scope)
|
||||
nav_links = nav_links(assigns.current_scope)
|
||||
prelaunch = ChesstrainerWeb.prelaunch?()
|
||||
|
||||
assigns =
|
||||
assigns
|
||||
|> assign(:nav_links, nav_links(assigns.current_scope))
|
||||
|> assign(:nav_links, nav_links)
|
||||
|> 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"""
|
||||
<header>
|
||||
@@ -80,41 +86,47 @@ defmodule ChesstrainerWeb.Layouts do
|
||||
<div class="nav-right">
|
||||
<.colour_mode_toggle />
|
||||
|
||||
<button
|
||||
class="menu-button"
|
||||
type="button"
|
||||
aria-expanded="false"
|
||||
aria-controls="nav-links"
|
||||
aria-label="Toggle menu"
|
||||
>
|
||||
<span></span>
|
||||
<span></span>
|
||||
</button>
|
||||
<%= if @show_nav do %>
|
||||
<button
|
||||
class="menu-button"
|
||||
type="button"
|
||||
aria-expanded="false"
|
||||
aria-controls="nav-links"
|
||||
aria-label="Toggle menu"
|
||||
>
|
||||
<span></span>
|
||||
<span></span>
|
||||
</button>
|
||||
|
||||
<div class="nav-links" id="nav-links">
|
||||
<%= for link <- @nav_links do %>
|
||||
<.link href={link.href}>{link.label}</.link>
|
||||
<% end %>
|
||||
<div class="nav-links" id="nav-links">
|
||||
<%= for link <- @nav_links do %>
|
||||
<.link href={link.href}>{link.label}</.link>
|
||||
<% end %>
|
||||
|
||||
<%= if @user do %>
|
||||
<details class="nav-user-menu">
|
||||
<summary>{@user.username}</summary>
|
||||
<div class="nav-user-menu-panel">
|
||||
<.link navigate={~p"/users/profile"}>Account</.link>
|
||||
<.link navigate={~p"/users/profile/settings"}>Settings</.link>
|
||||
<.link navigate={~p"/users/profile/data"}>Data</.link>
|
||||
<.link href={~p"/users/log-out"} method="delete">Log out</.link>
|
||||
</div>
|
||||
</details>
|
||||
<% else %>
|
||||
<.link href={~p"/users/log-in"}>Log in</.link>
|
||||
<.link href={~p"/users/register"}>Register</.link>
|
||||
<% end %>
|
||||
<%= if @user do %>
|
||||
<details class="nav-user-menu">
|
||||
<summary>{@user.username}</summary>
|
||||
<div class="nav-user-menu-panel">
|
||||
<.link navigate={~p"/users/profile"}>Account</.link>
|
||||
<.link navigate={~p"/users/profile/settings"}>Settings</.link>
|
||||
<.link navigate={~p"/users/profile/data"}>Data</.link>
|
||||
<.link href={~p"/users/log-out"} method="delete">Log out</.link>
|
||||
</div>
|
||||
</details>
|
||||
<% else %>
|
||||
<%= if !@prelaunch do %>
|
||||
<.link href={~p"/users/log-in"}>Log in</.link>
|
||||
<.link href={~p"/users/register"}>Register</.link>
|
||||
<% end %>
|
||||
<% end %>
|
||||
|
||||
<.pill_button variant={:nav} href={@start_href}>
|
||||
Start training <.icon_arrow />
|
||||
</.pill_button>
|
||||
</div>
|
||||
<%= if !@prelaunch do %>
|
||||
<.pill_button variant={:nav} href={@start_href}>
|
||||
Start training <.icon_arrow />
|
||||
</.pill_button>
|
||||
<% end %>
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
||||
</nav>
|
||||
</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
|
||||
|
||||
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)
|
||||
|
||||
@@ -11,7 +11,7 @@ defmodule ChesstrainerWeb.UserLive.Login do
|
||||
<%= if @current_scope do %>
|
||||
You need to reauthenticate to perform sensitive actions on your account.
|
||||
<% else %>
|
||||
<p class="settings-subtitle my-4">
|
||||
<p :if={!@prelaunch} class="settings-subtitle my-4">
|
||||
Don't have an account? <.link
|
||||
navigate={~p"/users/register"}
|
||||
class="font-semibold text-primary-text hover:underline"
|
||||
@@ -25,7 +25,7 @@ defmodule ChesstrainerWeb.UserLive.Login do
|
||||
:let={f}
|
||||
for={@form}
|
||||
id="login_form_password"
|
||||
action={~p"/users/log-in"}
|
||||
action={@login_action}
|
||||
phx-submit="submit_password"
|
||||
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)])
|
||||
|
||||
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
|
||||
|
||||
@impl true
|
||||
|
||||
@@ -74,19 +74,40 @@ defmodule ChesstrainerWeb.UserLive.Registration do
|
||||
end
|
||||
|
||||
@impl true
|
||||
def mount(_params, _session, %{assigns: %{current_scope: %{user: user}}} = socket)
|
||||
when not is_nil(user) do
|
||||
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
|
||||
{:ok, redirect(socket, to: ChesstrainerWeb.UserAuth.signed_in_path(socket))}
|
||||
end
|
||||
|
||||
def mount(_params, _session, socket) do
|
||||
defp do_mount(socket) do
|
||||
changeset = Accounts.change_user_registration(%User{}, %{}, validate_unique: false)
|
||||
|
||||
{:ok, assign_form(socket, changeset), temporary_assigns: [form: nil]}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("save", %{"user" => user_params}, socket) do
|
||||
defp save_registration(socket, user_params) do
|
||||
case Accounts.register_user(user_params) do
|
||||
{:ok, user} ->
|
||||
if user.mailing_list, do: MailingList.subscribe_user(user)
|
||||
@@ -110,10 +131,7 @@ defmodule ChesstrainerWeb.UserLive.Registration do
|
||||
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 prelaunch?, do: Application.get_env(:chesstrainer, :prelaunch, false)
|
||||
|
||||
defp assign_form(socket, %Ecto.Changeset{} = changeset) do
|
||||
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 :protect_from_forgery
|
||||
plug :put_secure_browser_headers
|
||||
plug ChesstrainerWeb.Plugs.Prelaunch
|
||||
plug :fetch_current_scope_for_user
|
||||
end
|
||||
|
||||
@@ -34,13 +35,17 @@ defmodule ChesstrainerWeb.Router do
|
||||
get "/terms-conditions", PageController, :terms_conditions
|
||||
post "/mailing-list", MailingListController, :create
|
||||
get "/mailing-list/confirm/:token", MailingListController, :confirm
|
||||
post "/admin/log-in", UserSessionController, :create_admin
|
||||
end
|
||||
|
||||
scope "/admin", ChesstrainerWeb do
|
||||
pipe_through [:browser, :noindex, :require_admin_user]
|
||||
|
||||
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/new", EndgameLive.Form, :new
|
||||
live "/endgames/:id", EndgameLive.Show, :show
|
||||
@@ -76,7 +81,10 @@ defmodule ChesstrainerWeb.Router do
|
||||
pipe_through [:browser, :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 "/train", EndgameLive.Train, :train
|
||||
live "/users/profile", UserLive.Profile, :account
|
||||
@@ -95,11 +103,15 @@ defmodule ChesstrainerWeb.Router do
|
||||
pipe_through [:browser]
|
||||
|
||||
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 "/endgames/:public_code/play", EndgameLive.Play, :play
|
||||
live "/users/register", UserLive.Registration, :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/:token", UserLive.ResetPassword, :edit
|
||||
end
|
||||
@@ -115,6 +127,7 @@ defmodule ChesstrainerWeb.Router do
|
||||
scope "/", ChesstrainerWeb do
|
||||
pipe_through :seo
|
||||
|
||||
get "/health", HealthController, :index
|
||||
get "/robots.txt", RobotsController, :index
|
||||
get "/sitemap.xml", SitemapController, :index
|
||||
end
|
||||
|
||||
@@ -212,6 +212,24 @@ defmodule ChesstrainerWeb.UserAuth do
|
||||
live "/profile", ProfileLive, :index
|
||||
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
|
||||
{:cont, mount_current_scope(socket, session)}
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user