Skip to content

Commit

Permalink
Merge branch 'email_2fa_fix_setting_init_nil' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
zephyranthes03 committed Jan 31, 2025
2 parents 150c33b + fe7dbb7 commit 045c6f4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 30 deletions.
71 changes: 42 additions & 29 deletions lib/recognizer_web/controllers/accounts/user_settings_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,30 @@ defmodule RecognizerWeb.Accounts.UserSettingsController do
end

def resend(conn, _params) do
conn =
conn
|> put_session(:two_factor_sent, false)
|> put_session(:two_factor_issue_time, System.system_time(:second))
user = Authentication.fetch_current_user(conn)
current_user = Accounts.get_new_two_factor_settings(user)

conn
|> put_flash(:info, "Two factor code has been resent")
|> redirect(to: Routes.user_settings_path(conn, :two_factor_init))
case current_user do
{:error, _} ->
conn
|> put_flash(:error, "Two factor setup expired or not yet initiated")
|> redirect(to: Routes.user_settings_path(conn, :edit))

{:ok, nil} ->
conn
|> put_flash(:error, "Two factor setup expired or not yet initiated")
|> redirect(to: Routes.user_settings_path(conn, :edit))

{:ok, _setting_user} ->
conn =
conn
|> put_session(:two_factor_sent, false)
|> put_session(:two_factor_issue_time, System.system_time(:second))

conn
|> put_flash(:info, "Two factor code has been resent")
|> redirect(to: Routes.user_settings_path(conn, :two_factor_init))
end
end

@doc """
Expand Down Expand Up @@ -72,36 +88,17 @@ defmodule RecognizerWeb.Accounts.UserSettingsController do
totp_app_url: Authentication.get_totp_app_url(user, seed)
)
else
conn = two_factor_init_email(conn, setting_user, user, method_atom)
current_time = System.system_time(:second)

IO.inspect(get_session(conn, :two_factor_sent, label: "two_factor_sent"))
conn = ensure_two_factor_issue_time(conn, current_time)
conn = two_factor_init_two_factor_sent(conn, setting_user, user, method_atom)

conn
|> render("confirm_two_factor_external.html")
end
end
end

def two_factor_init_email(conn, setting_user, user, method_atom) do
conn =
if get_session(conn, :two_factor_issue_time) == nil do
put_session(conn, :two_factor_issue_time, System.system_time(:second))
else
conn
end

two_factor_sent = get_session(conn, :two_factor_sent)

conn =
if two_factor_sent do
conn
else
conn
|> send_two_factor_notification(setting_user, user, method_atom)
|> put_session(:two_factor_sent, true)
end
end

@doc """
Confirming and saving a new two factor setup with user-provided code
"""
Expand Down Expand Up @@ -149,6 +146,22 @@ defmodule RecognizerWeb.Accounts.UserSettingsController do
end
end

def two_factor_init_two_factor_sent(conn, setting_user, user, method_atom) do
two_factor_sent = get_session(conn, :two_factor_sent)

conn_session =
if two_factor_sent do
conn
else
conn
|> send_two_factor_notification(setting_user, user, method_atom)

put_session(conn, :two_factor_sent, true)
end

conn_session
end

defp handle_two_factor_settings(conn, user, two_factor_code, method) do
two_factor_issue_time = get_session(conn, :two_factor_issue_time)

Expand Down
2 changes: 1 addition & 1 deletion lib/recognizer_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,6 @@ defmodule RecognizerWeb.Router do
get "/settings/two-factor/review", UserSettingsController, :review
get "/settings/two-factor", UserSettingsController, :two_factor_init
post "/settings/two-factor", UserSettingsController, :two_factor_confirm
get "/setting/two-factor/resend", UserSettingsController, :resend
get "/settings/two-factor/resend", UserSettingsController, :resend
end
end

0 comments on commit 045c6f4

Please sign in to comment.