为 Shopify 公共应用生成在线和离线访问令牌

时间:2021-02-07 18:51:04

标签: shopify koa shopify-app shopify-api shopify-api-node

Shopify 的样板应用程序执行此操作以生成访问令牌(默认为在线访问模式)

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}`);
      },
    })
  );

就我而言,我想要在线和离线令牌

  • 离线令牌将生成一次(如果商店未添加到我的数据库中),并将 accessMode 设置为离线并推送到数据库。
  • 当前登录用户的在线令牌。

有没有办法在一个流中生成两个令牌?

下面这个不行。 Koa 响应 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 个答案:

没有答案