Is there a standard way to execute some server side logic (ensure a metafield definition is created, ensure a fulfillment service is created, etc.) when an embedded app is installed? In the Node.js embedded app sample, the app uninstalled webhook handler is registered in the afterAuth callback. Would this be a good place to execute this code? I’ve also seen some people talk about creating a /install endpoint. Is that a better approach, and how would I hook that into the existing install/auth flow?
server.use(
createShopifyAuth({
async afterAuth(ctx) {
// Access token and shop available in ctx.state.shopify
const { shop, accessToken, scope } = ctx.state.shopify;
const host = ctx.query.host;
ACTIVE_SHOPIFY_SHOPS[shop] = scope;
const response = await Shopify.Webhooks.Registry.register({
shop,
accessToken,
path: "/webhooks",
topic: "APP_UNINSTALLED",
webhookHandler: async (topic, shop, body) =>
delete ACTIVE_SHOPIFY_SHOPS[shop],
});
if (!response.success) {
console.log(
`Failed to register APP_UNINSTALLED webhook: ${response.result}`
);
}
// Insert additional logic here
// Redirect to app with shop parameter upon auth
ctx.redirect(`/?shop=${shop}&host=${host}`);
},
})
);