目前,我正试图在不受保护的路由中传递密码重置生成的令牌,但是每当我执行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,
});下面是我如何用expressJwt构建unless
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只被解析为字符串a,:token的值实际上不会被传递。
我读过一些关于用regex或函数传递它的类似问题,但我自己也想不出来。This和this问题对于如何处理这个问题特别有用。
发布于 2019-04-11 15:19:40
您可以将正则表达式传递给除非,您可能已经意识到了这一点,因为您试图使用路径到RegExp。您也可以尝试自己编写正则表达式,并将其直接传递给除非。就您的情况而言,除非是这样的:
.unless({ path: [/\/images\//, /\/user\/password-reset\//, /^\/user\/reset\/[a-z0-9_-]*/]}));编辑: SO answer建议您不能在同一个数组中组合regex和字符串,所以我已经将所有路径转换为regex表达式。
发布于 2019-04-11 15:30:08
数组中有一个数组,用于传递到path中的unless值。
更改:
}).unless({ path: ['/images/', '/user/password-reset', unprotected ]}));至:
}).unless({ path: unprotected }));并将unprotected更改为包括其他路径:
const unprotected = [
'/images/',
'/user/password-reset',
pathToRegexp('/user/reset/:token', tokens),
];确保使用包含令牌(例如:/user/reset/123-some-real-looking-value )的路径进行测试
https://stackoverflow.com/questions/55634700
复制相似问题