node.js - 如何从多个文件访问商店 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}`);
});
});
解决方案
推荐阅读
- faunadb - 我怎样才能获得动物数据库文档的更改历史记录?
- excel - 将特定列从一个 Excel 工作表复制到另一个工作表
- variables - 为什么这会无缘无故地忽略我的变量?
- python - 使用可变默认参数,如何从函数外部访问对象?
- android - ExoPlayer 通知管理器,隐藏快退和快进按钮
- android - ItemTouchHelper.SimpleCallback 以编程方式滑动
- python - 使用 numpy.dot(m1, m2) 与 m1.dot(m2) 的矩阵乘法之间的差异
- amazon-web-services - 在 AWS ECS 上的 Docker 映像中运行 CloudWatch 代理失败
- python - Gale-Shapley 算法稳定性测试
- c - 找出字符串中最长单词的长度。C语言