Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

After login user should go to resend email form #9

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions lib/maker_passport_web/live/user_live/resend_email_confirmation.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
defmodule MakerPassportWeb.ResendEmailConfirmation do
use MakerPassportWeb, :live_view

alias MakerPassport.Accounts

def render(assigns) do
~H"""
<div class="mx-auto max-w-sm">
<.header class="text-center">
You have not confirmed your email address.
</.header>

<.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
</.button>
</:actions>
</.simple_form>
</div>
"""
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
1 change: 1 addition & 0 deletions lib/maker_passport_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 12 additions & 5 deletions lib/maker_passport_web/user_auth.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ 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)

conn
|> 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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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