我对Node.js的开发很陌生,目前在空闲时间正在开发一个宠物项目。混合passport-facebook和passport-jwt的最佳方式是什么?
到目前为止,我已使用passport和passport-jwt创建了JWT身份验证,并将其用于所有RESTful API。
现在我正在考虑将这种混合与某种Facebook身份验证混合使用,仍然希望使用令牌身份验证。
目前,这是怎么了产生和获得令牌:
exports.authenticate = function(req, res) {
User.findOne({
email: req.body.email
}, function(err, user) {
if (err)
return res.status(400).send(getErrorMessage(err));
if (!user) {
res.status(400).send({
success: false,
message: 'Authentication failed. User not found.'
});
} else {
if (user.checkPassword(req.body.password)) {
let token = jwt.encode(user, config.secretPhrase);
res.json({
success: true,
token: 'JWT ' + token
});
} else {
res.status(401).send({
success: false,
message: 'Authentication failed. Wrong password.'
});
}
}
});
};
app.route('/api/users/authenticate')
.post(user.authenticate);
,并验证我做了以下内容:
let user = require('../../app/controllers/user-controller');
app.route('/api/todos')
.get(user.validateLogin, todos.list)
.post(user.validateLogin, todos.create);
用户控制器:
exports.validateLogin = passport.authenticate('jwt', {
session: false
});
任何人可以建议一种干净的方式来混合这两种策略?我应该使用express-jwt? express-jwt和passport-jwt有什么区别?