这个验证工作正常,我得到一个重定向:
server.post(authPostRoute, passport.authenticate(
'local'
, { successRedirect: '/', failureRedirect: '/login' }
));
调用回调后,此身份验证挂起:
server.post(authPostRoute, passport.authenticate(
'local'
, function(){ console.log('Hitting the callback'); console.log(arguments)}
));
这会记录以下内容:
{ '0': null,
'1':
{ id: [Getter/Setter],
firstName: [Getter/Setter],
lastName: [Getter/Setter],
email: [Getter/Setter],
addedOn: [Getter/Setter],
active: [Getter/Setter],
password: [Getter/Setter] },
'2': undefined }
但是在整个文档 (http://passportjs.org/guide/authenticate/) 中,它看起来好像应该传递 req 和 res,但显然不是。然后是调用回调的代码:
node_modules\passport\lib\middleware\authenticate.js
strategy.success = function(user, info) {
if (callback) {
return callback(null, user, info);
}
不传递这些参数。我做错了什么?
最佳答案
好的,我昨天花了 9 个小时努力取消我的自定义身份验证并用 Passport 替换它。在让 node-orm 在请求之外公开模型和处理订购东西的流程之间,我有点筋疲力尽。代码示例是准确的,我只需要仔细阅读:
// traditional route handler, passed req/res
server.post(authPostRoute, function(req, res, next) {
// generate the authenticate method (the anonymous method) and
// associate it with the 'local' strategy
passport.authenticate('local', function(err, user, info) {
if (err) { return next(err); }
if (!user) { return res.redirect('/'); }
// req / res held in closure
req.logIn(user, function(err) {
if (err) { return next(err); }
return res.send(user);
});
})(req, res, next);
});
关于node.js - Passport 身份验证回调未通过 req 和 res,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21855650/