英文:
Setting up Auth0 in Express API: How do i export this constant in order to use it in routes files?
问题
我正在为API设置Auth0,并且已经按照文档成功使其工作,但我想要控制哪些端点受保护,而不是使用 app.use(jwtCheck)
。因此,我想要导出这里标为红色的jwtCheck常量:
以便在这里使用它(忽略Auth.isStorage,这是我要摆脱的自定义身份验证):
但它就是不起作用。我尝试了export、export default、module.exports,但它总是未定义。我做错了什么?
英文:
Im setting up Auth0 for an API and i managed to make it work following the docs, but i want to control which endpoints are protected instead of using app.use(jwtCheck)
. So i want to export the jwtCheck constant marked in red here:
To use it here (ignore the Auth.isStorage, its a custom auth im getting rid of):
But it just wont work. I tried export, export default, module.exports, but its always undefined. What am i doing wrong?
答案1
得分: 1
您已创建了一个循环依赖关系
> index.js
<- Routes
<- Routes/Item.js
<- index.js
只需将 jwtCheck
移动到它自己的模块(文件)中。
例如
// middleware/auth.js
import { auth } from "express-oauth2-jwt-bearer";
export const jwtCheck = auth({
audience: "http://localhost:3000", // 建议使用环境变量
issuerBaseURL: "https://...",
tokenSigningAlg: "RS256",
});
然后您可以在需要的地方导入它
// Routes/Item.js
import { jwtCheck } from "../middleware/auth";
英文:
You've created a circular dependency
> index.js
<- Routes
<- Routes/Item.js
<- index.js
Simply move the jwtCheck
to its own module (file).
For example
// middleware/auth.js
import { auth } from "express-oauth2-jwt-bearer";
export const jwtCheck = auth({
audience: "http://localhost:3000", // suggest you use env vars
issuerBaseURL: "https://...",
tokenSigningAlg: "RS256",
});
Then you can import it wherever required
// Routes/Item.js
import { jwtCheck } from "../middleware/auth";
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论