Shopify / shopify-express

“Get up and running quickly with Express.js and the Shopify API.”
MIT License
137 stars 87 forks source link

Async/Await or Promise in afterAuth #84

Open jstoeffler opened 6 years ago

jstoeffler commented 6 years ago

Hi,

Thanks for this great package! I'm working in an app, and I'm trying to save the store data (the store domain and the access token of the user) in a database.

I thought the best way to do this is in afterAuth, because you want to do this as early as possible in the process. The problem is that the database APIS are Promise/Async-Await based, and it seems like it's not possible to wait for afterAuth to return the redirection.

Perhaps my design is bad, or perhaps there's a way to combine afterAuth with an async middleware? Or is this something which is not supported?

janfabian commented 6 years ago

Hi, you can use async promise in afterAuth parameter.

Example:

const shopifyConfig = {
    host: config.shopify.host,
    apiKey: config.shopify.apiKey,
    secret: config.shopify.secret,
    scope: ['read_products, read_inventory'],
    shopStore: new MongoStrategy(),
    async afterAuth(request, response) {
        const { session: { accessToken, shop } } = request;

        slackNotification(shop);
        await registerAppShop(shop);
        await registerShopifyShop(shop, accessToken);
        await startCalculateVariantStatistics(shop, accessToken);
        await registerWebhooks(shop, accessToken);
        return response.redirect('/');
    },
};