Express Passport (node.js)错误处理

我已经研究了通过 这个问题 < em > Node.js + Express.js 应用程序的错误处理原则? 在节点中如何进行错误处理,但是我不确定当身份验证失败时护照在做什么。我有以下 LocalStrategy:

passport.use(new LocalStrategy({ usernameField: 'email', passwordField: 'password' },
function(email, password, next) {
 

User.find({email: UemOrUnm}, function(err, user){
if (err) { console.log('Error > some err'); return next(err); }
if (!user) { console.log('Error > no user'); return next('Incorrect login or password'); }


if (password != user.password) {
return next(Incorrect login or password);
}
return next(null, user);
});
}
));

在我看到‘ Error > some err’控制台打印输出之后,没有其他事情发生。我认为它应该继续在下一个路径上使用错误参数,但它似乎没有这样做。发生什么事了?

81874 次浏览

策略实现与 passport.authenticate一起工作,既验证请求,又处理成功/失败。

假设您正在使用这个路由(通过电子邮件地址和密码传递) :

app.post('/login', passport.authenticate('local', {
successRedirect: '/loggedin',
failureRedirect: '/login', // see text
failureFlash: true // optional, see text as well
});

这将调用策略中的代码,其中可能发生以下三种情况之一:

  1. 试图获取用户信息时发生了一个内部错误(假设数据库连接不见了) ; 这个错误将被传递: next(err); 这将由 Express 处理并生成一个 HTTP 500响应;
  2. 所提供的凭据是无效的(没有用户提供的电子邮件地址,或者密码是不匹配的) ; 在这种情况下,你不会生成一个错误,但你传递一个 false作为用户对象: next(null, false); 这将触发 failureRedirect(如果你不定义一个,一个 HTTP 401未授权响应将生成) ;
  3. 一切都检查完毕,您有一个有效的用户对象,所以您传递它: next(null, user); 这将触发 successRedirect;

如果身份验证无效(但不是内部错误) ,您可以在传递回调的同时传递额外的消息:

next(null, false, { message : 'invalid e-mail address or password' });

如果您已经使用了安装了 连接闪存中间件failureFlash 还有,那么提供的消息将存储在会话中,并且可以很容易地访问,例如,可以在模板中使用。

编辑: 也可以自己完全处理验证过程的结果(而不是 Passport 发送重定向或401) :

app.post('/login', function(req, res, next) {
passport.authenticate('local', function(err, user, info) {
if (err) {
return next(err); // will generate a 500 error
}
// Generate a JSON response reflecting authentication status
if (! user) {
return res.send({ success : false, message : 'authentication failed' });
}
// ***********************************************************************
// "Note that when using a custom callback, it becomes the application's
// responsibility to establish a session (by calling req.login()) and send
// a response."
// Source: http://passportjs.org/docs
// ***********************************************************************
req.login(user, loginErr => {
if (loginErr) {
return next(loginErr);
}
return res.send({ success : true, message : 'authentication succeeded' });
});
})(req, res, next);
});

您需要添加 req.logIn(function (err) { });并在回调函数中执行成功的重定向

Christian 说的是你需要添加函数

req.login(user, function(err){
if(err){
return next(err);
}
return res.send({success:true});
});

所以整个路线就是:

app.post('/login', function(req, res, next) {
passport.authenticate('local', function(err, user, info) {
if (err) {
return next(err); // will generate a 500 error
}
// Generate a JSON response reflecting authentication status
if (! user) {
return res.send(401,{ success : false, message : 'authentication failed' });
}
req.login(user, function(err){
if(err){
return next(err);
}
return res.send({ success : true, message : 'authentication succeeded' });
});
})(req, res, next);
});

来源: http://passportjs.org/guide/login/

过了一段时间,现在最正确的代码将是:

  passport.authenticate('local', (err, user, info) => {
if (err) {
return next(err); // will generate a 500 error
}
// Generate a JSON response reflecting authentication status
if (!user) {
return res.status(401).send({ error: 'Authentication failed' });
}
req.login(user, (err) => {
if (err) {
return next(err);
}
return res.status(202).send({ error: 'Authentication succeeded' });
});
});

我发现这个帖子非常有用!

Https://github.com/jaredhanson/passport-local/issues/2

您可以使用它来返回错误并以形式呈现它。

app.post('/login',
passport.authenticate('local', { successRedirect: '/home', failWithError: true }),
function(err, req, res, next) {
// handle error
return res.render('login-form');
}
);