I've developed a couple of Shopify custom apps for people using node.js, and it's been a lot of trial-and-error honestly. That trial and error have led me to the following code:
/* GET home page. */
router.get('/', adminMiddleware, function (req, res, next) {
const shop = req.query.shop;
if (req.query.session) {
// if we're here, that means the app was clicked on in the shopify admin panel
return res.send({hello: 'world'});
}
if (shop) {
// if we're here, that means this is install initialization
const state = nonce();
const redirectUri = forwardingAddress + '/shopify/callback';
const installUrl = 'https://' + shop +
'/admin/oauth/authorize?client_id=' + apiKey +
'&scope=' + scopes +
'&state=' + state +
'&redirect_uri=' + redirectUri;
res.cookie('state', state);
res.redirect(installUrl);
} else {
return res.status(400).send('Missing shop parameter. Please add ?shop=your-development-shop.myshopify.com to your request');
}
});
So, according to my trial and error, which I'm not confident about at all, I will end up at the root of my Shopify routes (as defined in the App URL setting on the Shopify partner dashboard) under 2 scenarios:
when someone is installing the app, in which case I will expect a
shop
in the query parameters, orwhen someone clicks on my app inside their Admin Panel of a Shopify store, in which case there will be a
req.query.session
variable set, and I can respond I guess with some html+css if I want to (the example just shows a simple JSON response).
But... is that right? Are those the two scenarios Shopify will be using my root route? And is checking for req.query.shop
or req.query.session
the proper way to distinguish between those two circumstances? I'm not sure I'm dealing with this stuff properly.