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

Delete reset token if email failure #290

Merged
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
52 changes: 41 additions & 11 deletions lib/teiserver/account/emails.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,35 @@
defmodule Teiserver.Account.Emails do
@moduledoc false
require Logger
alias Bamboo.Email
alias Teiserver.Helper.TimexHelper

def send_password_reset(user, code \\ nil) do
{code, email} = password_reset(user, code)
response = Teiserver.Mailer.deliver_now(email, response: true)

case response do
{:ok, _email, _response} ->
:ok

{:error, error} ->
case Teiserver.Account.delete_code(code) do
# the cursed path
{:error, err} ->
Logger.error(
"Failed to delete code #{inspect(code)} for user at #{user.email}: #{inspect(err)}"
)

_ ->
Logger.info(
"Deleted password reset token for user at #{user.email} because email failed"
)
end

{:error, error}
end
end

def password_reset(user, code \\ nil) do
# We need this to enable recreating the email if we know it
# should exist but at the same time we don't want
Expand Down Expand Up @@ -46,16 +73,19 @@ defmodule Teiserver.Account.Emails do
message_id = "<#{UUID.uuid1()}@#{Application.get_env(:teiserver, Teiserver)[:host]}>"
subject = Application.get_env(:teiserver, Teiserver)[:game_name] <> " - Password reset"

Email.new_email()
|> Email.to({user.name, user.email})
|> Email.from(
{Application.get_env(:teiserver, Teiserver.Mailer)[:noreply_name],
Teiserver.Mailer.noreply_address()}
)
|> Email.subject(subject)
|> Email.put_header("Date", date)
|> Email.put_header("Message-Id", message_id)
|> Email.html_body(html_body)
|> Email.text_body(text_body)
email =
Email.new_email()
|> Email.to({user.name, user.email})
|> Email.from(
{Application.get_env(:teiserver, Teiserver.Mailer)[:noreply_name],
Teiserver.Mailer.noreply_address()}
)
|> Email.subject(subject)
|> Email.put_header("Date", date)
|> Email.put_header("Message-Id", message_id)
|> Email.html_body(html_body)
|> Email.text_body(text_body)

{code, email}
end
end
2 changes: 1 addition & 1 deletion lib/teiserver/common/email_helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,6 @@ defmodule Teiserver.EmailHelper do
|> Email.put_header("Message-Id", message_id)
|> Email.html_body(html_body)
|> Email.text_body(text_body)
|> Teiserver.Mailer.deliver_now()
|> Teiserver.Mailer.deliver_now(response: true)
end
end
12 changes: 2 additions & 10 deletions lib/teiserver/data/cache_user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,8 @@ defmodule Teiserver.CacheUser do
:no_verify ->
verify_user(get_user_by_id(user.id))

{:ok, _} ->
{:ok, _, _} ->
:ok
# Logger.error("Email sent, response of #{Kernel.inspect response}")
end
end

Expand Down Expand Up @@ -339,7 +338,7 @@ defmodule Teiserver.CacheUser do
verify_user(get_user_by_id(user.id))
:ok

{:ok, _} ->
{:ok, _, _} ->
:ok
end
end
Expand Down Expand Up @@ -500,13 +499,6 @@ defmodule Teiserver.CacheUser do
:ok
end

def request_password_reset(user) do
db_user = Account.get_user!(user.id)

Teiserver.Account.Emails.password_reset(db_user)
|> Teiserver.Mailer.deliver_now()
end

def request_email_change(nil, _), do: nil

def request_email_change(user, new_email) do
Expand Down
2 changes: 0 additions & 2 deletions lib/teiserver_web/components/layouts/app.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@
current_user={@current_user}
/>

<.flash_group flash={@flash} />

<main>
<.breadcrumb_trail :if={assigns[:breadcrumb_trails]} trails={assigns[:breadcrumb_trails]} />

Expand Down
1 change: 1 addition & 0 deletions lib/teiserver_web/components/layouts/root.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
</script>
</head>
<body class="container-fluid px-0 d-flex flex-column">
<.flash_group flash={@flash} />
<%= @inner_content %>
</body>
</html>
19 changes: 14 additions & 5 deletions lib/teiserver_web/controllers/account/session_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,21 @@ defmodule TeiserverWeb.Account.SessionController do
|> render("forgot_password.html")

true ->
Account.Emails.password_reset(user)
|> Teiserver.Mailer.deliver_now()
case Teiserver.Account.Emails.send_password_reset(user) do
:ok ->
conn
|> put_flash(:success, "Password reset email sent out")
|> redirect(to: ~p"/login")

conn
|> put_flash(:success, "Password reset sent out")
|> redirect(to: "/login")
{:error, error} ->
Logger.error(
"Failed to send password reset email to user at #{user.email}: #{inspect(error)}"
)

conn
|> put_flash(:error, "Oops, something went wrong resetting the password")
|> redirect(to: ~p"/forgot_password")
end
end
end

Expand Down
21 changes: 15 additions & 6 deletions lib/teiserver_web/controllers/admin/user_controller.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule TeiserverWeb.Admin.UserController do
@moduledoc false
require Logger
use TeiserverWeb, :controller

alias Teiserver.{Account, Chat, Game}
Expand Down Expand Up @@ -379,12 +380,21 @@ defmodule TeiserverWeb.Admin.UserController do
|> redirect(to: ~p"/teiserver/admin/user")

{true, _} ->
Teiserver.Account.Emails.password_reset(user)
|> Teiserver.Mailer.deliver_now()
case Teiserver.Account.Emails.send_password_reset(user) do
:ok ->
conn
|> put_flash(:success, "Password reset email sent to user")
|> redirect(to: ~p"/teiserver/admin/user/#{user}")

conn
|> put_flash(:success, "Password reset email sent to user")
|> redirect(to: ~p"/teiserver/admin/user")
{:error, error} ->
Logger.error(
"Failed to send password reset email to user at #{user.email}: #{inspect(error)}"
)

conn
|> put_flash(:error, "Oops, something went wrong resetting the password")
|> redirect(to: ~p"/teiserver/admin/user/#{user}")
end
end
end

Expand Down Expand Up @@ -1069,7 +1079,6 @@ defmodule TeiserverWeb.Admin.UserController do

case Teiserver.Account.UserLib.has_access(user, conn) do
{true, _} ->

new_user =
Map.merge(user, %{
name: Ecto.UUID.generate(),
Expand Down
Loading