Skip to content

Commit

Permalink
BC Checkout (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
macifell authored May 30, 2024
1 parent f62ba74 commit 7dff888
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 5 deletions.
13 changes: 13 additions & 0 deletions lib/recognizer/bigcommerce.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,21 @@ defmodule Recognizer.BigCommerce do

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

def checkout_redirect_uri(user), do: home_redirect_uri() <> config(:login_path) <> generate_checkout_login_jwt(user)

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

defp generate_checkout_login_jwt(user) do
{:ok, token, _claims} =
user
|> Recognizer.Repo.preload(:bigcommerce_user)
|> jwt_claims()
|> Map.put("redirect_to", "/checkout")
|> Token.generate_and_sign(jwt_signer())

token
end

defp generate_login_jwt(user) do
{:ok, token, _claims} =
user
Expand Down
3 changes: 3 additions & 0 deletions lib/recognizer_web/authentication.ex
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ defmodule RecognizerWeb.Authentication do
"""
def login_redirect(conn, user) do
cond do
get_session(conn, :bc_checkout) ->
[external: BigCommerce.checkout_redirect_uri(user)]

get_session(conn, :bc) ->
[external: BigCommerce.login_redirect_uri(user)]

Expand Down
14 changes: 13 additions & 1 deletion lib/recognizer_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,21 @@ defmodule RecognizerWeb.Router do
plug :add_bc_to_session
end

defp add_bc_to_session(%{query_params: %{"bc" => "true", "checkout" => "true"}} = conn, _opts) do
if Recognizer.BigCommerce.enabled?() do
conn
|> put_session(:bc_checkout, true)
|> put_session(:bc, true)
else
conn
end
end

defp add_bc_to_session(%{query_params: %{"bc" => "true"}} = conn, _opts) do
if Recognizer.BigCommerce.enabled?() do
put_session(conn, :bc, true)
conn
|> delete_session(:bc_checkout)
|> put_session(:bc, true)
else
conn
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,42 @@ defmodule RecognizerWeb.Accounts.UserRegistrationControllerTest do

test "redirects to bigcommerce if already logged in", %{conn: conn} do
%{user: user} = insert(:bc_customer_user)
conn = conn |> log_in_user(user) |> get(Routes.user_registration_path(conn, :new, %{"bc" => "true"}))

assert redirected_to(conn) =~ "http://localhost/login/"
redirect =
conn
|> log_in_user(user)
|> get(Routes.user_registration_path(conn, :new, %{"bc" => "true"}))
|> redirected_to()

jwt_payload =
redirect
|> then(&Regex.named_captures(~r/\.(?<payload>.+)\./, &1))
|> then(fn %{"payload" => base64} -> base64 end)
|> Base.decode64!(padding: false)
|> Jason.decode!(keys: :atoms)

assert redirect =~ "http://localhost/login/"
refute :redirect_to in Map.keys(jwt_payload)
end

test "redirects to bigcommerce checkout if already logged in", %{conn: conn} do
%{user: user} = insert(:bc_customer_user)

redirect =
conn
|> log_in_user(user)
|> get(Routes.user_registration_path(conn, :new, %{"bc" => "true", "checkout" => "true"}))
|> redirected_to()

jwt_payload =
redirect
|> then(&Regex.named_captures(~r/\.(?<payload>.+)\./, &1))
|> then(fn %{"payload" => base64} -> base64 end)
|> Base.decode64!(padding: false)
|> Jason.decode!(keys: :atoms)

assert redirect =~ "http://localhost/login/"
assert %{redirect_to: "/checkout"} = jwt_payload
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,39 @@ defmodule RecognizerWeb.Accounts.UserSessionControllerTest do
end

test "redirects to bigcommerce if already logged in", %{conn: conn, user: user} do
conn = conn |> log_in_user(user) |> get(Routes.user_session_path(conn, :new, %{"bc" => "true"}))
assert redirected_to(conn) =~ "http://localhost/login/"
redirect =
conn
|> log_in_user(user)
|> get(Routes.user_session_path(conn, :new, %{"bc" => "true"}))
|> redirected_to()

jwt_payload =
redirect
|> then(&Regex.named_captures(~r/\.(?<payload>.+)\./, &1))
|> then(fn %{"payload" => base64} -> base64 end)
|> Base.decode64!(padding: false)
|> Jason.decode!(keys: :atoms)

assert redirect =~ "http://localhost/login/"
refute :redirect_to in Map.keys(jwt_payload)
end

test "redirects to bigcommerce checkout if already logged in", %{conn: conn, user: user} do
redirect =
conn
|> log_in_user(user)
|> get(Routes.user_session_path(conn, :new, %{"bc" => "true", "checkout" => "true"}))
|> redirected_to()

jwt_payload =
redirect
|> then(&Regex.named_captures(~r/\.(?<payload>.+)\./, &1))
|> then(fn %{"payload" => base64} -> base64 end)
|> Base.decode64!(padding: false)
|> Jason.decode!(keys: :atoms)

assert redirect =~ "http://localhost/login/"
assert %{redirect_to: "/checkout"} = jwt_payload
end
end

Expand Down

0 comments on commit 7dff888

Please sign in to comment.