Generate both online and offline access token for a Shopify public app

Viewed 828

Shopify's boilerplate app does this to generate an access token (online accessMode by default)

server.use(
    createShopifyAuth({
      apiKey: SHOPIFY_API_KEY,
      secret: SHOPIFY_API_SECRET,
      scopes: [SCOPES],

      async afterAuth(ctx) {
        // Access token and shop available in ctx.state.shopify
        const { shop } = ctx.state.shopify;

        // Redirect to app with shop parameter upon auth
        ctx.redirect(`/?shop=${shop}`);
      },
    })
  );

In my case, I want both online and offline token

  • Offline token will be generated once (if the store is not added to my database) with accessMode set to offline and pushed to the DB.
  • Online token for the current logged in user.

Is there a way to generate both tokens in a single flow?

This below doesn't work. Koa responds a 404 not found

server.use(function (ctx, next) {

    console.log(JSON.stringify(ctx.request.query.shop))
    if (ctx.request && ctx.request.query && ctx.request.query.shop) {
        if (isStoreAlreadyAdded(ctx.request.query.shop) === 0) {
            createShopifyAuth({
      apiKey: SHOPIFY_API_KEY,
      secret: SHOPIFY_API_SECRET,
      scopes: [SCOPES],
      accessMode: 'offline',

      async afterAuth(ctx) {
        // Access token and shop available in ctx.state.shopify
        const { shop } = ctx.state.shopify;

        // Redirect to app with shop parameter upon auth
        ctx.redirect(`/?shop=${shop}`);
      },
    })
        }
    } else {
        shopifyAuth({
            afterAuth(ctx) {
                const {shop, accessToken} = ctx.state.shopify;
                console.log('We did it!', accessToken);
                ctx.redirect(`/?shop=${shop}`);
            },
        })
    }
});
0 Answers
Related