Skip to content

Commit

Permalink
Make EnsureAuthenticatedLinks compatible with AppBridge 2
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillplatonov committed Jun 5, 2021
1 parent 8386580 commit a133064
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
21 changes: 19 additions & 2 deletions app/controllers/concerns/shopify_app/ensure_authenticated_links.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,26 @@ module EnsureAuthenticatedLinks

private

def splash_page
splash_page_with_params(shop: current_shopify_domain, host: params[:host])
end

def base_splash_page
root_path(return_to: request.fullpath)
end

def splash_page_with_params(params)
uri = URI(base_splash_page)
uri.query = CGI.parse(uri.query.to_s)
.symbolize_keys
.transform_values { |v| v.one? ? v.first : v }
.merge(params)
.to_query
uri.to_s
end

def redirect_to_splash_page
splash_page_path = root_path(return_to: request.fullpath, shop: current_shopify_domain)
redirect_to(splash_page_path)
redirect_to(splash_page)
rescue ShopifyApp::LoginProtection::ShopifyDomainNotFound => error
Rails.logger.warn("[ShopifyApp::EnsureAuthenticatedLinks] Redirecting to login: [#{error.class}] "\
"Could not determine current shop domain")
Expand Down
6 changes: 3 additions & 3 deletions test/controllers/concerns/ensure_authenticated_links_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ def current_shopify_domain
end
end

test 'redirects to splash page with a return_to and shop param if no session token is present' do
get :some_link, params: { shop: @shop_domain }
test 'redirects to splash page with a return_to, shop and host params if no session token is present' do
get :some_link, params: { shop: @shop_domain, host: 'test-host' }

expected_path = root_path(return_to: request.fullpath, shop: @shop_domain)
expected_path = "/?host=test-host&return_to=#{CGI.escape(request.fullpath)}&shop=#{@shop_domain}"

assert_redirected_to expected_path
end
Expand Down

0 comments on commit a133064

Please sign in to comment.