4 回答

TA贡献2037条经验 获得超6个赞
为了解决这个问题,您可以在中间件中进行管理
function authorized(request, response, next) {
passport.authenticate('jwt', { session: false, }, async (error, token) => {
if (error || !token) {
response.status(401).json({ message: 'Unauthorized Message' });
}
try {
console.log('token',token)
const user = await User.findOne(
{_id: token._id}
);
request.user = user;
} catch (error) {
next(error);
}
next();
})(request, response, next);
}
router.get(
'/',
authorized,
trimRequest.all,
user.getItems
)

TA贡献1784条经验 获得超2个赞
就我而言,我正在使用expiresIn
环境变量中的值。环境变量的值将是String
类型,如果我们将字符串值传递给expiresIn
,它将以毫秒为单位。因此,值3600被转换为3秒,到我将令牌从登录响应复制到auth标头时,该令牌已过期。

TA贡献1859条经验 获得超6个赞
用 passport.use('jwt',new JWTStrategy({opts},verify_callback(){})
在passport.js
添加回答
举报