Skip to content

Commit

Permalink
Merge branch 'bc-redirect-home' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
ah-s76 committed May 15, 2024
2 parents 8c993b1 + 3fd8590 commit cc1914b
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 32 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?: true

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
13 changes: 7 additions & 6 deletions lib/recognizer/bigcommerce.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ 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
15 changes: 3 additions & 12 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
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 cc1914b

Please sign in to comment.