1
votes

I am using shopify_app gem with rails 5. I want the shopify_app to work from a nested route. For eg: /shopify_app/login or '/shopify_app/auth/shopify/callback'.

I tried the solution given in gem documentation and put these lines in my routes.rb file:

mount ShopifyApp::Engine, at: 'shopify_app'  
get '/shopify_app' => 'settings#index'

Routes for shopify_app root Routes for shopify_app engine

But it didn't work this way. So I googled around and found a solution to make it work by doing some changes in an omniauth initializer.

Rails.application.config.middleware.use OmniAuth::Builder do
    provider :shopify, ShopifyApp.configuration.api_key, ShopifyApp.configuration.secret, scope: ShopifyApp.configuration.scope, callback_path: '/shopify_app/auth/shopify/callback'
end

Here are the app settings:

Embedded App SDK: enabled

App Url: https://salebait.com/settings

Whitelisted Redirection Url: https://salebait.com/shopify_app/auth/shopify/callback

Now the problem is when I install my app for the first time it redirects to my website's homepage (https://salebait.com) after installation but it should open https://salebait.com/settings page in store admin instead. And if I close the browser or logout from the store and come back again to the app it goes to https://salebait.com/login which is not a valid page since all app routes are wrapped inside /shopify_app/ so the login URL for the app should be https://salebait.com/shopify_app/login. Then I have to manually hit the app's login URL to make it work.

Please suggest some solution make my app routes work smoothly.

Edit: App redirects to wrong login page if try to open app in another browser.. Here is the error screen for that: enter image description here

Thanks.

1
It worked fine for me. The App installed and redirected all correctly based on the URL's you provided. There is an error in your App though, assuming some asset exists that does not in fact exist. Some defensive coding there and you might be off to the races. sale_bait_common_elems_asset = ShopifyAPI::Asset.find('snippets/sale_bait_common_elems.liquid', :params => {:theme_id => shop.active_theme})David Lazar
Hi @DavidLazar , when you installed the app, did it redirect you to shopify store admin and showed you app settings? or it went to salebait.com only? Can you please try again after logging out?shalini
As I mentioned. The flow was all fine. The only error that occurred was an assumption you made in your App that the resource looked for would be present.. and when it was not, you failed to catch the 404 exception so things blew up.David Lazar
Hi @DavidLazar , sorry to disturb you again. Could you please try reinstalling the app? I have removed the 404 error now. So once again, the issue is, app was installed fine, but when I opened my store in another browser and tried to access the app, it redirected to error page. Adding the error screen in question. Thanks for your help!!shalini

1 Answers

2
votes

If anyone else is looking for the solution, here it is: I was mounting the shopify_app engine under "/shopify_app" namespace. but it is the gem's default namespace so it won't override default routes.

So I changed the shopify_app namespace to "/app" and it worked smoothly.

mount ShopifyApp::Engine, at: 'app'  
get '/app' => 'settings#index'

Also changed the path in Omniauth builder and app settings.

Rails.application.config.middleware.use OmniAuth::Builder do
    provider :shopify, ShopifyApp.configuration.api_key, ShopifyApp.configuration.secret, scope: ShopifyApp.configuration.scope, callback_path: '/app/auth/shopify/callback'
end