2020-07-28 22:56:26 +02:00
|
|
|
defmodule PhilomenaWeb.Registration.EmailController do
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
|
|
|
alias Philomena.Users
|
|
|
|
|
|
|
|
def create(conn, %{"current_password" => password, "user" => user_params}) do
|
|
|
|
user = conn.assigns.current_user
|
|
|
|
|
|
|
|
case Users.apply_user_email(user, password, user_params) do
|
|
|
|
{:ok, applied_user} ->
|
|
|
|
Users.deliver_update_email_instructions(
|
|
|
|
applied_user,
|
|
|
|
user.email,
|
2024-06-06 22:28:35 +02:00
|
|
|
&url(~p"/registrations/email/#{&1}")
|
2020-07-28 22:56:26 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_flash(
|
2023-11-23 17:07:49 +01:00
|
|
|
:alert,
|
2020-07-28 22:56:26 +02:00
|
|
|
"A link to confirm your email change has been sent to the new address."
|
|
|
|
)
|
2024-06-06 22:28:35 +02:00
|
|
|
|> redirect(to: ~p"/registrations/edit")
|
2020-07-28 22:56:26 +02:00
|
|
|
|
|
|
|
{:error, _changeset} ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:error, "Failed to update email.")
|
2024-06-06 22:28:35 +02:00
|
|
|
|> redirect(to: ~p"/registrations/edit")
|
2020-07-28 22:56:26 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def show(conn, %{"id" => token}) do
|
|
|
|
case Users.update_user_email(conn.assigns.current_user, token) do
|
|
|
|
:ok ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Email changed successfully.")
|
2024-06-06 22:28:35 +02:00
|
|
|
|> redirect(to: ~p"/registrations/edit")
|
2020-07-28 22:56:26 +02:00
|
|
|
|
|
|
|
:error ->
|
|
|
|
conn
|
2023-11-23 17:07:49 +01:00
|
|
|
|> put_flash(:warning, "Email change link is invalid or it has expired.")
|
2024-06-06 22:28:35 +02:00
|
|
|
|> redirect(to: ~p"/registrations/edit")
|
2020-07-28 22:56:26 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|