3

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

0 回答 0