Skip to content

Commit

Permalink
add button for redirect to store home (#150)
Browse files Browse the repository at this point in the history
"continue shopping" button added for configurable return to home page.
  • Loading branch information
ah-s76 authored May 15, 2024
1 parent e01d642 commit f62ba74
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 36 deletions.
5 changes: 3 additions & 2 deletions config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ config :recognizer, Recognizer.BigCommerce,
client_secret: "bc_secret",
access_token: "bc_access_token",
store_hash: "bc_store_hash",
login_uri: "http://localhost/login/",
logout_uri: "http://localhost/logout",
store_home_uri: "http://localhost",
login_path: "/login",
logout_path: "/logout",
http_client: HTTPoison,
enabled?: false

Expand Down
5 changes: 3 additions & 2 deletions config/releases.exs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ config :recognizer, Recognizer.BigCommerce,
client_secret: recognizer_config["BIGCOMMERCE_CLIENT_SECRET"],
access_token: recognizer_config["BIGCOMMERCE_ACCESS_TOKEN"],
store_hash: recognizer_config["BIGCOMMERCE_STORE_HASH"],
login_uri: recognizer_config["BIGCOMMERCE_LOGIN_URI"],
logout_uri: recognizer_config["BIGCOMMERCE_LOGOUT_URI"],
store_home_uri: recognizer_config["BIGCOMMERCE_HOME_URI"],
login_path: recognizer_config["BIGCOMMERCE_LOGIN_PATH"],
logout_path: recognizer_config["BIGCOMMERCE_LOGOUT_PATH"],
http_client: HTTPoison,
enabled?: false

Expand Down
5 changes: 3 additions & 2 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ config :recognizer, Recognizer.BigCommerce,
client_secret: "bc_secret",
access_token: "bc_access_token",
store_hash: "bc_store_hash",
login_uri: "http://localhost/login/",
logout_uri: "http://localhost/logout",
store_home_uri: "http://localhost",
login_path: "/login/",
logout_path: "/logout",
http_client: HTTPoisonMock,
enabled?: true

Expand Down
10 changes: 4 additions & 6 deletions lib/recognizer/bigcommerce.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,11 @@ defmodule Recognizer.BigCommerce do
end
end

def login_redirect_uri(user) do
config(:login_uri) <> generate_login_jwt(user)
end
def home_redirect_uri(), do: config(:store_home_uri)

def logout_redirect_uri() do
config(:logout_uri)
end
def login_redirect_uri(user), do: home_redirect_uri() <> config(:login_path) <> generate_login_jwt(user)

def logout_redirect_uri(), do: home_redirect_uri() <> config(:logout_path)

defp generate_login_jwt(user) do
{:ok, token, _claims} =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ defmodule RecognizerWeb.Accounts.UserSettingsController do

alias Recognizer.Accounts
alias Recognizer.Accounts.Role
alias Recognizer.BigCommerce
alias RecognizerWeb.Authentication

@one_minute 60_000

plug :assign_email_and_password_changesets
plug :assign_common

plug Hammer.Plug,
[
Expand All @@ -25,13 +27,7 @@ defmodule RecognizerWeb.Accounts.UserSettingsController do
if Application.get_env(:recognizer, :redirect_url) && !get_session(conn, :bc) do
redirect(conn, external: Application.get_env(:recognizer, :redirect_url))
else
# disable phone/text 2fa methods for admins
is_admin =
conn
|> Authentication.fetch_current_user()
|> Role.admin?()

render(conn, "edit.html", allow_phone_methods: !is_admin)
render(conn, "edit.html")
end
end

Expand Down Expand Up @@ -196,4 +192,18 @@ defmodule RecognizerWeb.Accounts.UserSettingsController do
|> assign(:password_changeset, Accounts.change_user_password(user))
|> assign(:two_factor_changeset, Accounts.change_user_two_factor(user))
end

defp assign_common(conn, _opts) do
home_uri = BigCommerce.home_redirect_uri()

# disable phone/text 2fa methods for admins
is_admin =
conn
|> Authentication.fetch_current_user()
|> Role.admin?()

conn
|> assign(:redirect_home, home_uri)
|> assign(:allow_phone_methods, !is_admin)
end
end
21 changes: 5 additions & 16 deletions lib/recognizer_web/templates/accounts/user_settings/edit.html.eex
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
<div class="box">
<h2 class="title is-2 mb-5 has-text-centered-mobile">Log Out</h2>

<p>
This will log you out of this website, but <b>not</b> the system76.com
website, or any other third party websites you have logged in on.
</p>

<div class="buttons is-right mt-5">
<div class="control">
<%= link "Log Out", to: Routes.user_session_path(@conn, :delete), class: "button is-danger" %>
</div>
<div class="buttons is-centered mt-5">
<div class="control">
<%= link "Continue Shopping", to: @redirect_home, class: "button is-link" %>
</div>
</div>

Expand Down Expand Up @@ -211,8 +202,6 @@
every time you log in.
</p>

<% phone_allowed = Map.get(assigns, :allow_phone_methods, true) %>

<%= inputs_for f, :notification_preference, fn n -> %>
<div class="label mt-4">
Authentication Method Preference
Expand All @@ -225,7 +214,7 @@
Authenticator App
<% end %>

<%= if phone_allowed do %>
<%= if @allow_phone_methods do %>

<%= label class: "label" do %>
<%= radio_button n, :two_factor, "text" %>
Expand All @@ -241,7 +230,7 @@
</div>
<% end %>

<%= if phone_allowed do %>
<%= if @allow_phone_methods do %>
<p class="is-size-7">
Message and data rates may apply for text message and phone call
methods.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ defmodule RecognizerWeb.Accounts.UserSettingsControllerTest do
test "renders settings page", %{conn: conn} do
conn = get(conn, Routes.user_settings_path(conn, :edit))
response = html_response(conn, 200)
assert response =~ "Log Out</h2>"
assert response =~ "Update Profile</h2>"
assert response =~ "Text Message"
end

Expand Down

0 comments on commit f62ba74

Please sign in to comment.