2021-03-11 21:12:55 -05:00
|
|
|
defmodule CanneryWeb.UserSettingsController do
|
|
|
|
use CanneryWeb, :controller
|
2022-02-20 20:39:21 -05:00
|
|
|
import CanneryWeb.Gettext
|
2021-03-11 21:12:55 -05:00
|
|
|
alias Cannery.Accounts
|
2022-01-21 20:36:25 -05:00
|
|
|
alias CanneryWeb.{HomeLive, UserAuth}
|
2021-03-11 21:12:55 -05:00
|
|
|
|
|
|
|
plug :assign_email_and_password_changesets
|
|
|
|
|
|
|
|
def edit(conn, _params) do
|
2022-02-20 20:39:21 -05:00
|
|
|
render(conn, "edit.html", page_title: gettext("Settings"))
|
2021-03-11 21:12:55 -05:00
|
|
|
end
|
|
|
|
|
2022-05-05 23:26:29 -04:00
|
|
|
def update(%{assigns: %{current_user: user}} = conn, %{
|
|
|
|
"action" => "update_email",
|
|
|
|
"current_password" => password,
|
|
|
|
"user" => user_params
|
|
|
|
}) do
|
2021-03-11 21:12:55 -05:00
|
|
|
case Accounts.apply_user_email(user, password, user_params) do
|
|
|
|
{:ok, applied_user} ->
|
|
|
|
Accounts.deliver_update_email_instructions(
|
|
|
|
applied_user,
|
|
|
|
user.email,
|
|
|
|
&Routes.user_settings_url(conn, :confirm_email, &1)
|
|
|
|
)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_flash(
|
|
|
|
:info,
|
2022-02-09 00:13:01 -05:00
|
|
|
dgettext(
|
|
|
|
"prompts",
|
|
|
|
"A link to confirm your email change has been sent to the new address."
|
|
|
|
)
|
2021-03-11 21:12:55 -05:00
|
|
|
)
|
|
|
|
|> redirect(to: Routes.user_settings_path(conn, :edit))
|
|
|
|
|
|
|
|
{:error, changeset} ->
|
2022-05-05 23:26:29 -04:00
|
|
|
conn |> render("edit.html", email_changeset: changeset)
|
2021-03-11 21:12:55 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-05 23:26:29 -04:00
|
|
|
def update(%{assigns: %{current_user: user}} = conn, %{
|
|
|
|
"action" => "update_password",
|
|
|
|
"current_password" => password,
|
|
|
|
"user" => user_params
|
|
|
|
}) do
|
2021-03-11 21:12:55 -05:00
|
|
|
case Accounts.update_user_password(user, password, user_params) do
|
|
|
|
{:ok, user} ->
|
|
|
|
conn
|
2022-02-09 00:13:01 -05:00
|
|
|
|> put_flash(:info, dgettext("prompts", "Password updated successfully."))
|
2021-03-11 21:12:55 -05:00
|
|
|
|> put_session(:user_return_to, Routes.user_settings_path(conn, :edit))
|
|
|
|
|> UserAuth.log_in_user(user)
|
|
|
|
|
|
|
|
{:error, changeset} ->
|
2022-05-05 23:26:29 -04:00
|
|
|
conn |> render("edit.html", password_changeset: changeset)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update(
|
|
|
|
%{assigns: %{current_user: user}} = conn,
|
|
|
|
%{"action" => "update_locale", "user" => %{"locale" => locale}}
|
|
|
|
) do
|
|
|
|
case Accounts.update_user_locale(user, locale) do
|
|
|
|
{:ok, _user} ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, dgettext("prompts", "Language updated successfully."))
|
|
|
|
|> redirect(to: Routes.user_settings_path(conn, :edit))
|
|
|
|
|
|
|
|
{:error, changeset} ->
|
|
|
|
conn |> render("edit.html", locale_changeset: changeset)
|
2021-03-11 21:12:55 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-05 23:26:29 -04:00
|
|
|
def confirm_email(%{assigns: %{current_user: user}} = conn, %{"token" => token}) do
|
|
|
|
case Accounts.update_user_email(user, token) do
|
2021-03-11 21:12:55 -05:00
|
|
|
:ok ->
|
|
|
|
conn
|
2022-02-09 00:13:01 -05:00
|
|
|
|> put_flash(:info, dgettext("prompts", "Email changed successfully."))
|
2021-03-11 21:12:55 -05:00
|
|
|
|> redirect(to: Routes.user_settings_path(conn, :edit))
|
|
|
|
|
|
|
|
:error ->
|
|
|
|
conn
|
2022-02-09 00:13:01 -05:00
|
|
|
|> put_flash(
|
|
|
|
:error,
|
|
|
|
dgettext("errors", "Email change link is invalid or it has expired.")
|
|
|
|
)
|
2021-03-11 21:12:55 -05:00
|
|
|
|> redirect(to: Routes.user_settings_path(conn, :edit))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-02-12 00:34:11 -05:00
|
|
|
def delete(%{assigns: %{current_user: current_user}} = conn, %{"id" => user_id}) do
|
|
|
|
if user_id == current_user.id do
|
|
|
|
current_user |> Accounts.delete_user!(current_user)
|
2021-09-10 21:37:17 -04:00
|
|
|
|
|
|
|
conn
|
2022-02-09 00:13:01 -05:00
|
|
|
|> put_flash(:error, dgettext("prompts", "Your account has been deleted"))
|
2022-01-21 20:36:25 -05:00
|
|
|
|> redirect(to: Routes.live_path(conn, HomeLive))
|
2021-09-10 21:37:17 -04:00
|
|
|
else
|
|
|
|
conn
|
2022-02-09 00:13:01 -05:00
|
|
|
|> put_flash(:error, dgettext("errors", "Unable to delete user"))
|
2021-09-10 21:37:17 -04:00
|
|
|
|> redirect(to: Routes.user_settings_path(conn, :edit))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-05 23:26:29 -04:00
|
|
|
defp assign_email_and_password_changesets(%{assigns: %{current_user: user}} = conn, _opts) do
|
2021-03-11 21:12:55 -05:00
|
|
|
conn
|
|
|
|
|> assign(:email_changeset, Accounts.change_user_email(user))
|
|
|
|
|> assign(:password_changeset, Accounts.change_user_password(user))
|
2022-05-05 23:26:29 -04:00
|
|
|
|> assign(:locale_changeset, Accounts.change_user_locale(user))
|
2021-03-11 21:12:55 -05:00
|
|
|
end
|
|
|
|
end
|