首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Express Passport (node.js)错误处理

Express Passport (node.js)错误处理
EN

Stack Overflow用户
提问于 2013-03-30 04:44:23
回答 4查看 76.1K关注 0票数 71

我已经研究了如何通过this question 在节点中进行错误处理,但我不确定passport在身份验证失败时会做什么。我有以下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‘控制台打印输出后,没有发生任何其他事情。我认为它应该使用错误参数继续下一条路径,但它似乎没有做到这一点。到底怎么回事?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-03-30 05:13:37

该策略实现与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处理并生成HTTP500响应;
  2. 提供的凭据无效(没有具有提供的电子邮件地址的用户,或者密码不匹配);在这种情况下,您不会生成错误,但您可以将false作为user对象传递:next(null, false);这将触发failureRedirect (如果您没有定义,将生成HTTP401未授权响应);
  3. 一切都得到了验证,您有一个有效的user对象,因此传递它:next(null, user);这将触发user

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

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

如果您使用了failureFlash并安装了the connect-flash middleware,则提供的消息将存储在会话中,并且可以轻松访问,例如,在模板中使用。

编辑:也可以完全自己处理身份验证过程的结果(而不是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);
});
票数 171
EN

Stack Overflow用户

发布于 2015-05-13 04:00:37

克里斯蒂安所说的是你需要添加一个函数

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/

票数 22
EN

Stack Overflow用户

发布于 2015-02-21 01:44:58

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15711127

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档