Skip to content

Commit

Permalink
Login & logout in sync with bc
Browse files Browse the repository at this point in the history
  • Loading branch information
macifell committed Dec 5, 2023
1 parent 7d4d3e0 commit 7af6f43
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 8 deletions.
3 changes: 2 additions & 1 deletion config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ config :recognizer, Recognizer.BigCommerce,
client_secret: "bc_secret",
access_token: "bc_access_token",
store_hash: "bc_store_hash",
login_uri: "http://localhost/",
login_uri: "http://localhost/login",
logout_uri: "http://localhost/logout",
http_client: HTTPoison,
enabled?: true
1 change: 1 addition & 0 deletions config/releases.exs
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,6 @@ config :recognizer, Recognizer.BigCommerce,
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"],
http_client: HTTPoison,
enabled?: false
3 changes: 2 additions & 1 deletion config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ config :recognizer, Recognizer.BigCommerce,
client_secret: "bc_secret",
access_token: "bc_access_token",
store_hash: "bc_store_hash",
login_uri: "http://localhost/",
login_uri: "http://localhost/login",
logout_uri: "http://localhost/logout",
http_client: HTTPoisonMock,
enabled?: true
4 changes: 4 additions & 0 deletions lib/recognizer/bigcommerce.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ defmodule Recognizer.BigCommerce do
config(:login_uri) <> jwt
end

def logout_redirect_uri() do
config(:logout_uri)
end

defp jwt_claims(user) do
%{
"aud" => "BigCommerce",
Expand Down
6 changes: 4 additions & 2 deletions lib/recognizer_web/authentication.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ defmodule RecognizerWeb.Authentication do

{:ok, _user} ->
if BigCommerce.enabled?() && get_session(conn, :bc) do
log_in_bc_user(conn, user)
log_in_bc_user(conn, user, params)
else
redirect_opts = login_redirect(conn)

Expand All @@ -45,11 +45,13 @@ defmodule RecognizerWeb.Authentication do
end
end

defp log_in_bc_user(conn, user) do
defp log_in_bc_user(conn, user, params) do
jwt = BigCommerce.generate_login_jwt(user)

conn
|> clear_session()
|> Guardian.Plug.sign_in(user, params)
|> put_session(:bc, true)
|> redirect(external: BigCommerce.login_redirect_uri(jwt))
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule RecognizerWeb.Accounts.UserSessionController do
use RecognizerWeb, :controller

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

def new(conn, %{"bc" => "true"}) do
Expand Down Expand Up @@ -39,6 +40,13 @@ defmodule RecognizerWeb.Accounts.UserSessionController do
end
end

def delete(conn, %{"bc" => "true"}) do
conn
|> put_session(:bc, true)
|> Authentication.log_out_user()
|> redirect(external: BigCommerce.logout_redirect_uri())
end

def delete(conn, _params) do
conn
|> Authentication.conditional_flash(:info, "Logged out successfully.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,22 @@ defmodule RecognizerWeb.Accounts.UserSettingsController do

plug :assign_email_and_password_changesets

def edit(conn, %{"bc" => "true"}) do
conn
|> put_session(:bc, true)
|> edit(%{})
end

def edit(conn, _params) do
if Application.get_env(:recognizer, :redirect_url) do
redirect(conn, external: Application.get_env(:recognizer, :redirect_url))
else
render(conn, "edit.html")
cond do
get_session(conn, :bc) ->
render(conn, "edit.html")

Application.get_env(:recognizer, :redirect_url) ->
redirect(conn, external: Application.get_env(:recognizer, :redirect_url))

true ->
render(conn, "edit.html")
end
end

Expand Down

0 comments on commit 7af6f43

Please sign in to comment.