philomena/lib/philomena_web/controllers/registration/email_controller.ex

45 lines
1.2 KiB
Elixir
Raw Normal View History

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}")
)
conn
|> put_flash(
2023-11-23 17:07:49 +01:00
:alert,
"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")
{:error, _changeset} ->
conn
|> put_flash(:error, "Failed to update email.")
2024-06-06 22:28:35 +02:00
|> redirect(to: ~p"/registrations/edit")
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")
: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")
end
end
end