diff --git a/config/dev.exs b/config/dev.exs index 51de6f4..5f33f6c 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -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 diff --git a/config/releases.exs b/config/releases.exs index 54594bc..9e5a2f5 100644 --- a/config/releases.exs +++ b/config/releases.exs @@ -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?: true diff --git a/config/test.exs b/config/test.exs index 441e975..8ee8c37 100644 --- a/config/test.exs +++ b/config/test.exs @@ -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 diff --git a/lib/recognizer/bigcommerce.ex b/lib/recognizer/bigcommerce.ex index 1869d4b..ff35473 100644 --- a/lib/recognizer/bigcommerce.ex +++ b/lib/recognizer/bigcommerce.ex @@ -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", diff --git a/lib/recognizer_web/authentication.ex b/lib/recognizer_web/authentication.ex index 6b70177..47c8764 100644 --- a/lib/recognizer_web/authentication.ex +++ b/lib/recognizer_web/authentication.ex @@ -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) @@ -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 diff --git a/lib/recognizer_web/controllers/accounts/user_session_controller.ex b/lib/recognizer_web/controllers/accounts/user_session_controller.ex index c9b2b89..3c137ba 100644 --- a/lib/recognizer_web/controllers/accounts/user_session_controller.ex +++ b/lib/recognizer_web/controllers/accounts/user_session_controller.ex @@ -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 @@ -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.") diff --git a/lib/recognizer_web/controllers/accounts/user_settings_controller.ex b/lib/recognizer_web/controllers/accounts/user_settings_controller.ex index 28f0ab0..a5e27fd 100644 --- a/lib/recognizer_web/controllers/accounts/user_settings_controller.ex +++ b/lib/recognizer_web/controllers/accounts/user_settings_controller.ex @@ -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