【发布时间】:2019-09-02 05:16:18
【问题描述】:
我目前正尝试在我的 unprotected 路由中传递我的密码重置生成的令牌,但是每当我执行我的 GET 请求时,我都会收到一个401 Unauthorized request。
我尝试包含包 Path-to-RegExp 并构造一个单独的数组路由,但它不起作用:
let tokens = [];
const unprotected = [
pathToRegexp('/user/reset/:token', tokens),
];
我的 password-reset 令牌在单独的服务中生成并在控制器中调用:
const token = crypto.randomBytes(20).toString('hex');
user.update({
resetPasswordToken: token,
resetPasswordExpires: Date.now() + 360000,
});
这是我使用unless 构建expressJwt 的方式:
app.use(expressJwt({
secret: process.env.SECRET_BEARER,
getToken: req => {
MY TOKEN AUTHORISATION CODE IS PLACED HERE.
}
}).unless({ path: ['/images/', '/user/password-reset', unprotected ]}));
我的问题是,每当我尝试创建未经身份验证的路由(例如 .unless({path: ['/images/', '/user/password-reset', '/user/reset/:token' ]}));)时,路由 /user/reset/:token 仅被解析为字符串,而 :token 的值实际上并未传递。
我已经阅读了一些关于使用正则表达式或函数传递它的类似问题,但我自己无法弄清楚。 This 和 this 问题对于如何解决问题特别有用。
【问题讨论】:
-
tokens和unprotected如果你console.log他们有什么价值? -
[ /^\/user\/reset\/([^\/]+?)(?:\/)?$/i ] [ { name: 'token', prefix: ' /',分隔符:'/',可选:false,重复:false,模式:'[^\\/]+?' } ]
标签: javascript node.js regex express authentication