shopify_app: Infinite redirect between /login, Home#index, and /granted_storage_access
I’m creating a new app and ran into this issue after installing the app in my test store, didn’t customize any Shopify related code.
The infinite loop starts when I click the app on the store’s list of installed apps.
Versions:
- ruby 2.6.5
- rails 6.0.3.2
- shopify_app 14.1.0
- ngrok 2.3.35
Rails server logs that shows the infinite loop:
Started GET "/?hmac=0f82d72949292546c12c4a9c173c8099d04f47f53897ad78b8b60a9d05fba9ff&locale=en&session=f64b9b51cb8fd00bd412d4e5dd4db4b0ec6108e077f5bc8ce824bd00d113f1c1&shop=storeurl.myshopify.com×tamp=1598059348" for 2804:14c:8782:9312:6538:5840:a987:d051 at 2020-08-21 22:22:29 -0300
Processing by HomeController#index as HTML
Parameters: {"hmac"=>"0f82d72949292546c12c4a9c173c8099d04f47f53897ad78b8b60a9d05fba9ff", "locale"=>"en", "session"=>"f64b9b51cb8fd00bd412d4e5dd4db4b0ec6108e077f5bc8ce824bd00d113f1c1", "shop"=>"storeurl.myshopify.com", "timestamp"=>"1598059348"}
Redirected to http://abc.sa.ngrok.io/login?return_to=%2F%3Fhmac%3D0f82d72949292546c12c4a9c173c8099d04f47f53897ad78b8b60a9d05fba9ff%26locale%3Den%26session%3Df64b9b51cb8fd00bd412d4e5dd4db4b0ec6108e077f5bc8ce824bd00d113f1c1%26shop%3Dstoreurl.myshopify.com%26timestamp%3D1598059348&shop=storeurl.myshopify.com
Completed 302 Found in 43ms (ActiveRecord: 0.0ms | Allocations: 7089)
Started GET "/login?return_to=%2F%3Fhmac%3D0f82d72949292546c12c4a9c173c8099d04f47f53897ad78b8b60a9d05fba9ff%26locale%3Den%26session%3Df64b9b51cb8fd00bd412d4e5dd4db4b0ec6108e077f5bc8ce824bd00d113f1c1%26shop%3Dstoreurl.myshopify.com%26timestamp%3D1598059348&shop=storeurl.myshopify.com" for 2804:14c:8782:9312:6538:5840:a987:d051 at 2020-08-21 22:22:29 -0300
Processing by ShopifyApp::SessionsController#new as HTML
Parameters: {"return_to"=>"/?hmac=0f82d72949292546c12c4a9c173c8099d04f47f53897ad78b8b60a9d05fba9ff&locale=en&session=f64b9b51cb8fd00bd412d4e5dd4db4b0ec6108e077f5bc8ce824bd00d113f1c1&shop=storeurl.myshopify.com×tamp=1598059348", "shop"=>"storeurl.myshopify.com"}
Rendering /Users/cayomedeiros/.rvm/gems/ruby-2.6.5@store-reviewer/gems/shopify_app-14.1.0/app/views/shopify_app/sessions/request_storage_access.html.erb
Rendered /Users/cayomedeiros/.rvm/gems/ruby-2.6.5@store-reviewer/gems/shopify_app-14.1.0/app/views/shopify_app/partials/_layout_styles.html.erb (Duration: 0.6ms | Allocations: 248)
Rendered /Users/cayomedeiros/.rvm/gems/ruby-2.6.5@store-reviewer/gems/shopify_app-14.1.0/app/views/shopify_app/partials/_typography_styles.html.erb (Duration: 0.3ms | Allocations: 114)
Rendered /Users/cayomedeiros/.rvm/gems/ruby-2.6.5@store-reviewer/gems/shopify_app-14.1.0/app/views/shopify_app/partials/_card_styles.html.erb (Duration: 0.2ms | Allocations: 113)
Rendered /Users/cayomedeiros/.rvm/gems/ruby-2.6.5@store-reviewer/gems/shopify_app-14.1.0/app/views/shopify_app/partials/_button_styles.html.erb (Duration: 0.4ms | Allocations: 184)
Rendered /Users/cayomedeiros/.rvm/gems/ruby-2.6.5@store-reviewer/gems/shopify_app-14.1.0/app/views/shopify_app/sessions/request_storage_access.html.erb (Duration: 39.4ms | Allocations: 12755)
Completed 200 OK in 45ms (Views: 44.2ms | ActiveRecord: 0.0ms | Allocations: 14612)
Started GET "/granted_storage_access?return_to=%2F%3Fhmac%3D0f82d72949292546c12c4a9c173c8099d04f47f53897ad78b8b60a9d05fba9ff%26locale%3Den%26session%3Df64b9b51cb8fd00bd412d4e5dd4db4b0ec6108e077f5bc8ce824bd00d113f1c1%26shop%3Dstoreurl.myshopify.com%26timestamp%3D1598059348&shop=storeurl.myshopify.com" for 2804:14c:8782:9312:6538:5840:a987:d051 at 2020-08-21 22:22:29 -0300
Processing by ShopifyApp::SessionsController#granted_storage_access as HTML
Parameters: {"return_to"=>"/?hmac=0f82d72949292546c12c4a9c173c8099d04f47f53897ad78b8b60a9d05fba9ff&locale=en&session=f64b9b51cb8fd00bd412d4e5dd4db4b0ec6108e077f5bc8ce824bd00d113f1c1&shop=storeurl.myshopify.com×tamp=1598059348", "shop"=>"storeurl.myshopify.com"}
Redirected to http://abc.sa.ngrok.io/?hmac=0f82d72949292546c12c4a9c173c8099d04f47f53897ad78b8b60a9d05fba9ff&locale=en&session=f64b9b51cb8fd00bd412d4e5dd4db4b0ec6108e077f5bc8ce824bd00d113f1c1&shop=storeurl.myshopify.com×tamp=1598059348
Completed 302 Found in 1ms (ActiveRecord: 0.0ms | Allocations: 399)
Started GET "/?hmac=0f82d72949292546c12c4a9c173c8099d04f47f53897ad78b8b60a9d05fba9ff&locale=en&session=f64b9b51cb8fd00bd412d4e5dd4db4b0ec6108e077f5bc8ce824bd00d113f1c1&shop=storeurl.myshopify.com×tamp=1598059348" for 2804:14c:8782:9312:6538:5840:a987:d051 at 2020-08-21 22:22:29 -0300
Processing by HomeController#index as HTML
Parameters: {"hmac"=>"0f82d72949292546c12c4a9c173c8099d04f47f53897ad78b8b60a9d05fba9ff", "locale"=>"en", "session"=>"f64b9b51cb8fd00bd412d4e5dd4db4b0ec6108e077f5bc8ce824bd00d113f1c1", "shop"=>"storeurl.myshopify.com", "timestamp"=>"1598059348"}
Redirected to http://abc.sa.ngrok.io/login?return_to=%2F%3Fhmac%3D0f82d72949292546c12c4a9c173c8099d04f47f53897ad78b8b60a9d05fba9ff%26locale%3Den%26session%3Df64b9b51cb8fd00bd412d4e5dd4db4b0ec6108e077f5bc8ce824bd00d113f1c1%26shop%3Dstoreurl.myshopify.com%26timestamp%3D1598059348&shop=storeurl.myshopify.com
Completed 302 Found in 2ms (ActiveRecord: 0.0ms | Allocations: 576)
Started GET "/login?return_to=%2F%3Fhmac%3D0f82d72949292546c12c4a9c173c8099d04f47f53897ad78b8b60a9d05fba9ff%26locale%3Den%26session%3Df64b9b51cb8fd00bd412d4e5dd4db4b0ec6108e077f5bc8ce824bd00d113f1c1%26shop%3Dstoreurl.myshopify.com%26timestamp%3D1598059348&shop=storeurl.myshopify.com" for 2804:14c:8782:9312:6538:5840:a987:d051 at 2020-08-21 22:22:29 -0300
Processing by ShopifyApp::SessionsController#new as HTML
Parameters: {"return_to"=>"/?hmac=0f82d72949292546c12c4a9c173c8099d04f47f53897ad78b8b60a9d05fba9ff&locale=en&session=f64b9b51cb8fd00bd412d4e5dd4db4b0ec6108e077f5bc8ce824bd00d113f1c1&shop=storeurl.myshopify.com×tamp=1598059348", "shop"=>"storeurl.myshopify.com"}
Rendering /Users/cayomedeiros/.rvm/gems/ruby-2.6.5@store-reviewer/gems/shopify_app-14.1.0/app/views/shopify_app/sessions/request_storage_access.html.erb
Rendered /Users/cayomedeiros/.rvm/gems/ruby-2.6.5@store-reviewer/gems/shopify_app-14.1.0/app/views/shopify_app/partials/_layout_styles.html.erb (Duration: 0.0ms | Allocations: 6)
Rendered /Users/cayomedeiros/.rvm/gems/ruby-2.6.5@store-reviewer/gems/shopify_app-14.1.0/app/views/shopify_app/partials/_typography_styles.html.erb (Duration: 0.0ms | Allocations: 5)
Rendered /Users/cayomedeiros/.rvm/gems/ruby-2.6.5@store-reviewer/gems/shopify_app-14.1.0/app/views/shopify_app/partials/_card_styles.html.erb (Duration: 0.0ms | Allocations: 5)
Rendered /Users/cayomedeiros/.rvm/gems/ruby-2.6.5@store-reviewer/gems/shopify_app-14.1.0/app/views/shopify_app/partials/_button_styles.html.erb (Duration: 0.0ms | Allocations: 5)
Rendered /Users/cayomedeiros/.rvm/gems/ruby-2.6.5@store-reviewer/gems/shopify_app-14.1.0/app/views/shopify_app/sessions/request_storage_access.html.erb (Duration: 5.7ms | Allocations: 2300)
Completed 200 OK in 9ms (Views: 6.1ms | ActiveRecord: 0.0ms | Allocations: 3116)
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 21
- Comments: 64 (14 by maintainers)
Hi @tanema 👋 Thanks for your reply, I really appreciate your help.
As you suggested, I’ve checked the
return_toparam in our OAuth call and can see that it’s simply set to the root URL of our Rails application. It doesn’t appear, therefore, that the problem is caused by sending the user back to the login page after successful authentication.In an effort to simplify the issue, I’ve created a brand new Rails app running the latest version the
shopify_appgem. With the exception of adding myserverless.socialURL to theconfig.hostsarray (config/environments/development.rb), I haven’t changed any of the code. This is a fresh Rails app after having runrails generate shopify_app.I’m sorry to report that, even when running this minimal test-case, the redirect issue is still present in Google Chrome:
@tanema Would you mind cloning my minimal Rails app and running it locally in Google Chrome? You’ll obviously need to set
ENV['SHOPIFY_API_KEY']andENV['SHOPIFY_API_SECRET']and somehow forward your localhost to the internet (ngrokorlocaltunnelfor example). I expect this will be enough to allow you to reproduce the issue.Thanks again for all your help and support 🙏
Here’s ready to use sample of Shopify app with JWT and Hotwire integration: https://github.com/kirillplatonov/shopify-hotwire-sample
Just created today (12/Jan) a bare-bones shopify_app using the latest versions of everything using the flag with-cookie-authentication. I can confirm that, when it tries to embed the app, it goes into an infinite loop between
GET /login | 200 OK GET / | 302 Found GET /granted_storage_access 302 Found
Versions: ruby-2.7.0 Rails 6.1.1 shopify_app 16.1.0 Chrome 87.0.4280.141 on Ubuntu 20.04 LTS
Steps:
Disabled same site cookies in chrome://flags/#same-site-by-default-cookies, but no luck.
In the past (a couple of months ago) I have created an app with the same procedure w/o any problem.
Another solution would be setting old
cookies_same_site_protectionvalue in rails v6.1.1:Hi @petebof, I believe in your particular case the issue is with rails v>=6.1 - we’re currently updating the gem to work with that version of rails. I’ll post updates here.
Hey @onahkenneth, can you please confirm whether your app’s
home_controller.rbinherits fromAuthenticatedControllerorApplicationController? If it is authenticated, you’ll want to replace it with the unauthenticated version to stop the infinite loop.Hey @paulomarg, thanks for your reply:
config/initializers/shopify_app.rb
What is the version of the shopify_app gem being used? shopify_app (17.0.2)
Home Controller
Note:
I changed the home controller to the one you provided and can confirm that the app is rendered in the iframe.!!! 🎉
The only caveat is that I would need to define some things for the shopify_api to fetch the products, but I can now work on my app!
THANKS
Yes @marisveide Rails 6.1 made some config changes in how it handles
SameSitecookies, so using a previous version’s configs would indeed work around the issue until the gem is fixed.If you have
Then there must be something else going on. Is it possible that you have config in your nginx setup that is altering your cookies, for instance a
proxy_cookie_pathsetting?Some further info on why this was so seemingly random: In the third party service we are using it was possible for the user to “accidentally” change his API key (which we store in the app DB). The invalid key then resulted in the ActiveResource::UnauthorizedAccess response. resulting in the behaviour described above.
As a workaround I specifically rescued these calls now inside the controller clearing the Key from our DB in response. This way the app customer can at least enter the app again and enter his new key.
Cheers, Dave
Same here, @derrickrc, I also stopped the developing any feature/improvement on an app because of this issue.
We have been getting lots of support inquiries as well due to similar error. We are considering switching to JWT but that’s quite buggy right now.
@tanema Same here. All of a sudden this day, we experienced this issue in one of our production apps. We hosted it also in Heroku.
ruby (2.5.3) rails (5.2.1.1) shopify_app (12.0.0) shopify_api (9.1.0) omniauth-shopify-oauth2 (2.2.2)
Hi @tanema 👋 Thanks for your reply, it’s super-helpful to see the exact steps you’re taking to setup and run the Rails app. After a bit of Googling, I’ve discovered that the
shopify connectandshopify servecommands are part of the Shopify CLI tool, something that I wasn’t aware of and haven’t been using until now. It’s brilliant!To my delight, I’ve found that booting the application using
shopify serverather thanbundle exec rails server, allows me to visit the Shopify app in Google Chrome without the redirect issue. Looking at the code forshopify serve, I think it’s simply runningrails server, so I don’t think there’s anything too different there.Another key difference is the (automatic) use of ngrok to forward my localhost as, until now, I’ve been forwarding using localtunnel. I wonder if that somehow makes a difference, though it wouldn’t explain why we’re seeing the redirect issue in production (hosted on Heroku) 🤔
In summary, now that you’ve shared a method to boot the app without the redirect issue, I can start to unpick the differences and hopefully have that “eureka” moment. I’ll report back here when I know more. Thanks for all your help!
Okay I still cannot reproduce this and this is what I have done
config.hostsbecause i do not need that hostshopify connectto get my app config and populate my .env fileshopify srvwhich does the following for meI am using
Chrome: Version 84.0.4147.135 ruby: 2.6.5 rails (6.0.3.2) shopify_app (14.3.0) shopify_api (9.2.0) omniauth-shopify-oauth2 (2.2.2)
Is there any chance that you are using extra strict cookie policies in your browsers or extensions for cookies, or even ad blockers that might be preventing this?