Ad

Change Engine Routes For Shopify_app Gem

- 1 answer

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 rootRoutes 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.

Ad

Answer

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
Ad
source: stackoverflow.com
Ad