2021-03-11 21:12:55 -05:00
|
|
|
defmodule CanneryWeb.UserRegistrationController do
|
|
|
|
use CanneryWeb, :controller
|
2022-02-09 00:13:01 -05:00
|
|
|
import CanneryWeb.Gettext
|
2023-02-04 16:46:37 -05:00
|
|
|
alias Cannery.{Accounts, Accounts.Invites}
|
2022-02-12 02:28:27 -05:00
|
|
|
alias CanneryWeb.{Endpoint, HomeLive}
|
2021-03-11 21:12:55 -05:00
|
|
|
|
2021-09-10 22:22:18 -04:00
|
|
|
def new(conn, %{"invite" => invite_token}) do
|
2023-02-04 16:46:37 -05:00
|
|
|
if Invites.valid_invite_token?(invite_token) do
|
|
|
|
conn |> render_new(invite_token)
|
2021-09-10 22:22:18 -04:00
|
|
|
else
|
|
|
|
conn
|
2022-02-09 00:13:01 -05:00
|
|
|
|> put_flash(:error, dgettext("errors", "Sorry, this invite was not found or expired"))
|
2022-02-12 02:28:27 -05:00
|
|
|
|> redirect(to: Routes.live_path(Endpoint, HomeLive))
|
2021-09-10 22:22:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-11 21:12:55 -05:00
|
|
|
def new(conn, _params) do
|
2021-09-10 22:22:18 -04:00
|
|
|
if Accounts.allow_registration?() do
|
|
|
|
conn |> render_new()
|
|
|
|
else
|
|
|
|
conn
|
2022-02-09 00:13:01 -05:00
|
|
|
|> put_flash(:error, dgettext("errors", "Sorry, public registration is disabled"))
|
2022-02-12 02:28:27 -05:00
|
|
|
|> redirect(to: Routes.live_path(Endpoint, HomeLive))
|
2021-09-10 22:22:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# renders new user registration page
|
2023-02-04 16:46:37 -05:00
|
|
|
defp render_new(conn, invite_token \\ nil) do
|
2022-02-20 20:39:21 -05:00
|
|
|
render(conn, "new.html",
|
2022-11-23 20:46:41 -05:00
|
|
|
changeset: Accounts.change_user_registration(),
|
2023-02-04 16:46:37 -05:00
|
|
|
invite_token: invite_token,
|
2022-02-20 20:39:21 -05:00
|
|
|
page_title: gettext("Register")
|
|
|
|
)
|
2021-03-11 21:12:55 -05:00
|
|
|
end
|
|
|
|
|
2021-09-10 22:22:18 -04:00
|
|
|
def create(conn, %{"user" => %{"invite_token" => invite_token}} = attrs) do
|
2023-02-04 16:46:37 -05:00
|
|
|
if Invites.valid_invite_token?(invite_token) do
|
|
|
|
conn |> create_user(attrs, invite_token)
|
2021-09-10 22:22:18 -04:00
|
|
|
else
|
|
|
|
conn
|
2022-02-09 00:13:01 -05:00
|
|
|
|> put_flash(:error, dgettext("errors", "Sorry, this invite was not found or expired"))
|
2022-02-12 02:28:27 -05:00
|
|
|
|> redirect(to: Routes.live_path(Endpoint, HomeLive))
|
2021-09-10 22:22:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create(conn, attrs) do
|
|
|
|
if Accounts.allow_registration?() do
|
|
|
|
conn |> create_user(attrs)
|
|
|
|
else
|
|
|
|
conn
|
2022-02-09 00:13:01 -05:00
|
|
|
|> put_flash(:error, dgettext("errors", "Sorry, public registration is disabled"))
|
2022-02-12 02:28:27 -05:00
|
|
|
|> redirect(to: Routes.live_path(Endpoint, HomeLive))
|
2021-09-10 22:22:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-02-04 16:46:37 -05:00
|
|
|
defp create_user(conn, %{"user" => user_params}, invite_token \\ nil) do
|
|
|
|
case Accounts.register_user(user_params, invite_token) do
|
2021-03-11 21:12:55 -05:00
|
|
|
{:ok, user} ->
|
2022-02-11 22:37:41 -05:00
|
|
|
Accounts.deliver_user_confirmation_instructions(
|
|
|
|
user,
|
|
|
|
&Routes.user_confirmation_url(conn, :confirm, &1)
|
|
|
|
)
|
2021-03-11 21:12:55 -05:00
|
|
|
|
|
|
|
conn
|
2022-02-12 02:28:27 -05:00
|
|
|
|> put_flash(:info, dgettext("prompts", "Please check your email to verify your account"))
|
|
|
|
|> redirect(to: Routes.user_session_path(Endpoint, :new))
|
2021-03-11 21:12:55 -05:00
|
|
|
|
2023-02-04 16:46:37 -05:00
|
|
|
{:error, :invalid_token} ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:error, dgettext("errors", "Sorry, this invite was not found or expired"))
|
|
|
|
|> redirect(to: Routes.live_path(Endpoint, HomeLive))
|
|
|
|
|
2021-03-11 21:12:55 -05:00
|
|
|
{:error, %Ecto.Changeset{} = changeset} ->
|
2023-02-04 16:46:37 -05:00
|
|
|
conn |> render("new.html", changeset: changeset, invite_token: invite_token)
|
2021-03-11 21:12:55 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|