memEx/lib/memex_web/controllers/user_registration_controller.ex

78 lines
2.3 KiB
Elixir
Raw Permalink Normal View History

2022-07-25 19:31:54 -04:00
defmodule MemexWeb.UserRegistrationController do
use MemexWeb, :controller
import MemexWeb.Gettext
2023-06-04 00:11:09 -04:00
alias Ecto.Changeset
2023-02-04 17:22:06 -05:00
alias Memex.{Accounts, Accounts.Invites}
2022-02-25 21:53:04 -05:00
def new(conn, %{"invite" => invite_token}) do
2023-02-04 17:22:06 -05:00
if Invites.valid_invite_token?(invite_token) do
conn |> render_new(invite_token)
2022-02-25 21:53:04 -05:00
else
conn
2023-04-14 19:19:58 -04:00
|> put_flash(:error, dgettext("errors", "sorry, this invite was not found or expired"))
2023-04-13 23:29:29 -04:00
|> redirect(to: ~p"/")
2022-02-25 21:53:04 -05:00
end
end
2021-03-11 21:12:55 -05:00
def new(conn, _params) do
2022-02-25 21:53:04 -05:00
if Accounts.allow_registration?() do
conn |> render_new()
else
conn
2023-04-14 19:19:58 -04:00
|> put_flash(:error, dgettext("errors", "sorry, public registration is disabled"))
2023-04-13 23:29:29 -04:00
|> redirect(to: ~p"/")
2022-02-25 21:53:04 -05:00
end
end
# renders new user registration page
2023-02-04 17:22:06 -05:00
defp render_new(conn, invite_token \\ nil) do
2023-04-13 23:29:29 -04:00
render(conn, :new,
2022-11-23 21:15:52 -05:00
changeset: Accounts.change_user_registration(),
2023-02-04 17:22:06 -05:00
invite_token: invite_token,
2022-11-17 22:30:01 -05:00
page_title: gettext("register")
2022-02-25 21:53:04 -05:00
)
2021-03-11 21:12:55 -05:00
end
2022-02-25 21:53:04 -05:00
def create(conn, %{"user" => %{"invite_token" => invite_token}} = attrs) do
2023-02-04 17:22:06 -05:00
if Invites.valid_invite_token?(invite_token) do
conn |> create_user(attrs, invite_token)
2022-02-25 21:53:04 -05:00
else
conn
2023-04-14 19:19:58 -04:00
|> put_flash(:error, dgettext("errors", "sorry, this invite was not found or expired"))
2023-04-13 23:29:29 -04:00
|> redirect(to: ~p"/")
2022-02-25 21:53:04 -05:00
end
end
def create(conn, attrs) do
if Accounts.allow_registration?() do
conn |> create_user(attrs)
else
conn
2023-04-14 19:19:58 -04:00
|> put_flash(:error, dgettext("errors", "sorry, public registration is disabled"))
2023-04-13 23:29:29 -04:00
|> redirect(to: ~p"/")
2022-02-25 21:53:04 -05:00
end
end
2023-02-04 17:22:06 -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-25 21:53:04 -05:00
Accounts.deliver_user_confirmation_instructions(
user,
2023-04-13 23:29:29 -04:00
fn token -> url(MemexWeb.Endpoint, ~p"/users/confirm/#{token}") end
2022-02-25 21:53:04 -05:00
)
2021-03-11 21:12:55 -05:00
conn
2023-02-04 17:22:06 -05:00
|> put_flash(:info, dgettext("prompts", "please check your email to verify your account"))
2023-04-13 23:29:29 -04:00
|> redirect(to: ~p"/users/log_in")
2021-03-11 21:12:55 -05:00
2023-02-04 17:22:06 -05:00
{:error, :invalid_token} ->
conn
|> put_flash(:error, dgettext("errors", "sorry, this invite was not found or expired"))
2023-04-13 23:29:29 -04:00
|> redirect(to: ~p"/")
2023-02-04 17:22:06 -05:00
2023-06-04 00:11:09 -04:00
{:error, %Changeset{} = changeset} ->
conn |> render(:new, changeset: changeset, invite_token: invite_token)
2021-03-11 21:12:55 -05:00
end
end
end