2020-07-30 00:49:51 +00:00
|
|
|
defmodule WikiWeb.UserRegistrationController do
|
|
|
|
use WikiWeb, :controller
|
|
|
|
|
|
|
|
alias Wiki.Accounts
|
|
|
|
alias Wiki.Accounts.User
|
|
|
|
alias WikiWeb.UserAuth
|
|
|
|
|
2020-08-02 22:35:14 +00:00
|
|
|
plug :check_registration_enabled
|
|
|
|
|
|
|
|
def check_registration_enabled(conn, _opts) do
|
|
|
|
if Application.get_env(:wiki, :registration_enabled, true) do
|
|
|
|
conn
|
|
|
|
else
|
|
|
|
conn
|
|
|
|
|> send_resp(403, "Forbidden")
|
|
|
|
|> halt()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-30 00:49:51 +00:00
|
|
|
def new(conn, _params) do
|
|
|
|
changeset = Accounts.change_user_registration(%User{})
|
|
|
|
render(conn, "new.html", changeset: changeset)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create(conn, %{"user" => user_params}) do
|
|
|
|
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, user_params)
|
|
|
|
|
|
|
|
{:error, %Ecto.Changeset{} = changeset} ->
|
|
|
|
render(conn, "new.html", changeset: changeset)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|