From f9d6fdd244761108e47a689e7ff3f8a168ba6d7c Mon Sep 17 00:00:00 2001 From: Zoey Lan Date: Mon, 8 Jul 2024 20:28:48 +0000 Subject: [PATCH] Fix unified admin install path for spin --- CHANGELOG.md | 1 + app/controllers/shopify_app/sessions_controller.rb | 3 ++- lib/shopify_app/utils.rb | 9 +++++++++ test/shopify_app/utils_test.rb | 12 ++++++++++++ 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8079c99b4..051fa487d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ Unreleased - Remove old translation keys for `enable_cookies_*`, `top_level_interaction_*` and `request_storage_access_*` [#1865](https://github.com/Shopify/shopify_app/pull/1865) - Add invalid id token handling for `current_shopify_domain` method [#1868](https://github.com/Shopify/shopify_app/pull/1868) - Keep original path and params when redirecting deep links to embed [#1869](https://github.com/Shopify/shopify_app/pull/1869) +- Fix managed install path for SPIN environments [#1877](https://github.com/Shopify/shopify_app/pull/1877) 22.2.1 (May 6,2024) ---------- diff --git a/app/controllers/shopify_app/sessions_controller.rb b/app/controllers/shopify_app/sessions_controller.rb index 2e5c977f6..7293b2c51 100644 --- a/app/controllers/shopify_app/sessions_controller.rb +++ b/app/controllers/shopify_app/sessions_controller.rb @@ -52,7 +52,8 @@ def authenticate def start_install shop_name = sanitized_shop_name.split(".").first - install_path = "https://admin.shopify.com/store/#{shop_name}/oauth/install?client_id=#{ShopifyApp.configuration.api_key}" + unified_admin_path = ShopifyApp::Utils.unified_admin_path(shop_name) + install_path = "#{unified_admin_path}/oauth/install?client_id=#{ShopifyApp.configuration.api_key}" redirect_to(install_path, allow_other_host: true) end diff --git a/lib/shopify_app/utils.rb b/lib/shopify_app/utils.rb index 03b28d36e..0a7c3bc27 100644 --- a/lib/shopify_app/utils.rb +++ b/lib/shopify_app/utils.rb @@ -39,6 +39,15 @@ def shop_login_url(shop:, host:, return_to:) url.to_s end + def unified_admin_path(shop) + spin_env = ENV.fetch("SPIN_FQDN", nil) + if spin_env + "https://admin.web.#{spin_env}/store/#{shop}" + else + "https://admin.shopify.com/store/#{shop}" + end + end + private def myshopify_domain diff --git a/test/shopify_app/utils_test.rb b/test/shopify_app/utils_test.rb index 19b85ed80..1d2b7dfd0 100644 --- a/test/shopify_app/utils_test.rb +++ b/test/shopify_app/utils_test.rb @@ -74,4 +74,16 @@ class UtilsTest < ActiveSupport::TestCase assert_nil ShopifyApp::Utils.sanitize_shop_domain(bad_url) end end + + test "#unified_admin_path returns the path to shop" do + expected = "https://admin.shopify.com/store/my-shop" + assert_equal expected, ShopifyApp::Utils.unified_admin_path("my-shop") + end + + test "#unified_admin_path returns the path to shop with spin env" do + ENV["SPIN_FQDN"] = "my.spin.dev" + expected = "https://admin.web.my.spin.dev/store/my-shop" + assert_equal expected, ShopifyApp::Utils.unified_admin_path("my-shop") + ENV["SPIN_FQDN"] = nil + end end