【问题标题】:In express-jwt is there any way option similar to req.isAuthenticated() similar to passportjs?在 express-jwt 中是否有任何类似于 req.isAuthenticated() 的方式选项类似于 passportjs?
【发布时间】:2015-01-18 14:27:51
【问题描述】:
我想查看用户是否在中间件中通过了“jwt”身份验证。有没有类似req.isAuthenticated() 类似passportjs的方法?
module.exports = function(app){
return function(req, res, next){
// How to implement the below step?
var isAuthenticated = app.use(jwt({secret: app.get('jwtTokenSecret')}))
if(isAuthenticated){
// do some logic
}
}
}
【问题讨论】:
标签:
node.js
express
express-jwt
【解决方案1】:
是的,有!
1) 您可以使用 express-jwt 模块。查看https://github.com/auth0/express-jwt
2) 你可以这样做:
//get the authorization token from the request headers, or from a cookie
if (req.headers && req.headers.authorization) {
var parts = req.headers.authorization.split(' ');
if (parts.length == 2) {
var scheme = parts[0];
var credentials = parts[1];
if (/^Bearer$/i.test(scheme)) {
token = credentials;
}
}
}
else if(req.cookies && req.cookies.token)
{
token = req.cookies.token;
}
...
jwt.verify(token, config.secrets.session, function(err, decoded) {
//isAuthenticated is in here
});