diff --git a/lib/maker_passport_web/controllers/user_session_controller.ex b/lib/maker_passport_web/controllers/user_session_controller.ex index 7421106..8050bba 100644 --- a/lib/maker_passport_web/controllers/user_session_controller.ex +++ b/lib/maker_passport_web/controllers/user_session_controller.ex @@ -24,7 +24,7 @@ defmodule MakerPassportWeb.UserSessionController do if user = Accounts.get_user_by_email_and_password(email, password) do conn |> put_flash(:info, info) - |> UserAuth.log_in_user(user, user_params) + |> UserAuth.log_in_user(user, user_params, info) else # In order to prevent user enumeration attacks, don't disclose whether the email is registered. conn diff --git a/lib/maker_passport_web/live/user_live/resend_email_confirmation.ex b/lib/maker_passport_web/live/user_live/resend_email_confirmation.ex new file mode 100644 index 0000000..baf722f --- /dev/null +++ b/lib/maker_passport_web/live/user_live/resend_email_confirmation.ex @@ -0,0 +1,45 @@ +defmodule MakerPassportWeb.ResendEmailConfirmation do + use MakerPassportWeb, :live_view + + alias MakerPassport.Accounts + + def render(assigns) do + ~H""" +
+ <.header class="text-center"> + You have not confirmed your email address. + + + <.simple_form for={@form} id="resend_email_form" phx-submit="resend_email"> + <.input field={@form[:email]} type="email" placeholder="Email" required /> + <:actions> + <.button phx-disable-with="Sending..." class="w-full"> + Resend email confirmation Link + + + +
+ """ + end + + def mount(_params, _session, socket) do + {:ok, assign(socket, form: to_form(%{}, as: "user"))} + end + + def handle_event("resend_email", %{"user" => %{"email" => email}}, socket) do + if user = Accounts.get_user_by_email(email) do + Accounts.deliver_user_confirmation_instructions( + user, + &url(~p"/users/confirm/#{&1}") + ) + end + + info = + "We have resent you an email confirmation link." + + {:noreply, + socket + |> put_flash(:info, info) + |> redirect(to: ~p"/")} + end +end diff --git a/lib/maker_passport_web/router.ex b/lib/maker_passport_web/router.ex index d338d13..842feee 100644 --- a/lib/maker_passport_web/router.ex +++ b/lib/maker_passport_web/router.ex @@ -69,6 +69,7 @@ defmodule MakerPassportWeb.Router do live_session :require_authenticated_user, on_mount: [{MakerPassportWeb.UserAuth, :ensure_authenticated}] do + live "/users/email_confirmation", ResendEmailConfirmation, :index live "/users/settings", UserSettingsLive, :edit live "/users/settings/confirm_email/:token", UserSettingsLive, :confirm_email diff --git a/lib/maker_passport_web/user_auth.ex b/lib/maker_passport_web/user_auth.ex index 782b384..9ee19e7 100644 --- a/lib/maker_passport_web/user_auth.ex +++ b/lib/maker_passport_web/user_auth.ex @@ -29,7 +29,7 @@ defmodule MakerPassportWeb.UserAuth do disconnected on log out. The line can be safely removed if you are not using LiveView. """ - def log_in_user(conn, user, params \\ %{}) do + def log_in_user(conn, user, params \\ %{}, info \\ nil) do token = Accounts.generate_user_session_token(user) user_return_to = get_session(conn, :user_return_to) @@ -37,7 +37,7 @@ defmodule MakerPassportWeb.UserAuth do |> renew_session() |> put_token_in_session(token) |> maybe_write_remember_me_cookie(token, params) - |> redirect(to: user_return_to || signed_in_path(conn)) + |> redirect(to: user_return_to || signed_in_path(conn, user, info)) end defp maybe_write_remember_me_cookie(conn, token, %{"remember_me" => "true"}) do @@ -172,7 +172,7 @@ defmodule MakerPassportWeb.UserAuth do socket = mount_current_user(socket, session) if socket.assigns.current_user do - {:halt, Phoenix.LiveView.redirect(socket, to: signed_in_path(socket))} + {:halt, Phoenix.LiveView.redirect(socket, to: signed_in_path(socket.assigns.current_user, socket))} else {:cont, socket} end @@ -192,7 +192,7 @@ defmodule MakerPassportWeb.UserAuth do def redirect_if_user_is_authenticated(conn, _opts) do if conn.assigns[:current_user] do conn - |> redirect(to: signed_in_path(conn)) + |> redirect(to: signed_in_path(conn.assigns.current_user, conn)) |> halt() else conn @@ -229,5 +229,12 @@ defmodule MakerPassportWeb.UserAuth do defp maybe_store_return_to(conn), do: conn - defp signed_in_path(_conn), do: ~p"/" + def signed_in_path(_conn, _user, "Account created successfully!"), do: ~p"/" + + def signed_in_path(conn, user, _), do: signed_in_path(conn, user) + + def signed_in_path(_conn, %{confirmed_at: nil}), do: ~p"/users/email_confirmation" + + def signed_in_path(_conn, _user), do: ~p"/" + end