Add configurable prevention of user registration

Should be done appropriately soon.
This commit is contained in:
Thelonius Kort
2023-01-31 18:16:28 +01:00
parent 54b609185d
commit c5853fc2aa
4 changed files with 29 additions and 4 deletions

View File

@ -24,9 +24,6 @@
<.link href={~p"/users/log_out"} method="delete">Log out</.link>
</li>
<% else %>
<li>
<.link href={~p"/users/register"}>Register</.link>
</li>
<li>
<.link href={~p"/users/log_in"}>Log in</.link>
</li>

View File

@ -7,11 +7,13 @@ defmodule OutlookWeb.UserLoginLive do
<.header class="text-center">
Sign in to account
<:subtitle>
<%= unless System.get_env("DISABLE_REGISTRATION") do %>
Don't have an account?
<.link navigate={~p"/users/register"} class="font-semibold text-brand hover:underline">
Sign up
</.link>
for an account now.
<% end %>
</:subtitle>
</.header>

View File

@ -0,0 +1,20 @@
defmodule Outlook.PreventRegistration do
import Plug.Conn
import Phoenix.Controller
def prevent_registration(conn, _) do
if System.get_env("DISABLE_REGISTRATION") && is_registration_path(conn) do
conn
|> put_flash(:error, "User Registration is disabled.")
|> redirect(to: "/users/log_in")
|> halt()
else
conn
end
end
defp is_registration_path(conn) do
"/users/register" == current_path(conn, %{})
end
end

View File

@ -3,6 +3,8 @@ defmodule OutlookWeb.Router do
import OutlookWeb.UserAuth
import Outlook.PreventRegistration
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
@ -18,6 +20,10 @@ defmodule OutlookWeb.Router do
plug :put_layout, {OutlookWeb.Layouts, :public}
end
pipeline :check_registration do
plug :prevent_registration
end
pipeline :api do
plug :accepts, ["json"]
end
@ -56,7 +62,7 @@ defmodule OutlookWeb.Router do
## Authentication routes
scope "/", OutlookWeb do
pipe_through [:browser, :redirect_if_user_is_authenticated]
pipe_through [:browser, :redirect_if_user_is_authenticated, :check_registration]
live_session :redirect_if_user_is_authenticated,
on_mount: [{OutlookWeb.UserAuth, :redirect_if_user_is_authenticated}] do