meta/lib/recycledcloud_web/controllers/user_registration_controlle...

51 lines
1.3 KiB
Elixir

defmodule RecycledCloudWeb.UserRegistrationController do
use RecycledCloudWeb, :controller
alias RecycledCloud.Accounts
alias RecycledCloud.Accounts.User
alias RecycledCloudWeb.UserAuth
defp is_registration_enabled?() do
Application.get_env(:recycledcloud, :enable_registration)
end
def new(conn, _params) do
changeset = Accounts.change_user_registration(%User{})
with_notice = unless is_registration_enabled?() do
conn
|> put_flash(:error, "Registration is disabled for the time being.")
else
conn
end
with_notice
|> render("new.html", changeset: changeset)
end
def create(conn, %{"user" => user_params}) do
changeset = Accounts.change_user_registration(%User{})
unless is_registration_enabled?() do
conn
|> redirect(to: Routes.user_registration_path(conn, :new))
else
case Accounts.register_user(user_params) do
{:ok, user} ->
{:ok, _} =
Accounts.deliver_user_confirmation_instructions(
user,
&Routes.user_confirmation_url(conn, :confirm, &1)
)
conn
|> put_flash(:info, "User created successfully.")
|> UserAuth.log_in_user(user)
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "new.html", changeset: changeset)
end
end
end
end