首页 > 解决方案 > 如何从多个文件访问商店 url 和访问令牌?

问题描述

我按照本教程https://shopify.dev/tutorials/build-a-shopify-app-with-node-and-react进行操作,现在我有了一个想要开发的应用程序库,但是,我如何访问shop url (example.myshopify.com) 和在 server.js 文件上从另一个文件生成的 accessToken。

有没有办法使商店网址(example.myshopify.com)和 accessToken 成为全局变量或发送到数据库,以便我可以在所有文件上访问它们?我对这一切都很陌生,所以不确定我在做什么。

编辑:

app.prepare().then(() => {
  const server = new Koa();
  server.use(session({ secure: true, sameSite: 'none' }, server));
  server.keys = [SHOPIFY_API_SECRET_KEY];

  server.use(
    createShopifyAuth({
      apiKey: SHOPIFY_API_KEY,
      secret: SHOPIFY_API_SECRET_KEY,
      scopes: ['read_themes', 'write_themes'],
      async afterAuth(ctx) {
        const { shop, accessToken } = ctx.session;
        ctx.cookies.set('shopOrigin', shop, {
          httpOnly: false,
          secure: true,
          sameSite: 'none'
        });
                  console.log(`${shop}`);
        console.log(`${accessToken}`);


        ctx.redirect('/');
      },

    }),

  );


  server.use(verifyRequest());
  server.use(async (ctx) => {
    await handle(ctx.req, ctx.res);
    ctx.respond = false;
    ctx.res.statusCode = 200;
    return
  });

  server.listen(port, () => {
    console.log(`> Ready on http://localhost:${port}`);
  });

});

标签: node.jsreactjsshopifyshopify-appshopify-api-node

解决方案


推荐阅读