相关疑难解决方法(0)

Express Passport(node.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);
    });
  }
));
Run Code Online (Sandbox Code Playgroud)

在我看到'错误>一些错误的'控制台打印输出后,没有其他事情发生.我认为它应该继续在下一个带有错误参数的路径上,但它似乎没有这样做.这是怎么回事?

node.js express passport.js

65
推荐指数
2
解决办法
6万
查看次数

很难在express.js中理解'next/next()'

这是一个例子:

// Configuration
app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
});
Run Code Online (Sandbox Code Playgroud)

(等等.)

app.get('/memo', function(req, res) {
  console.log("index");
  Memo.find({}, function(err, data) {
    if(err) return next(err);
    res.render('index', { memos: data });
  });
});
Run Code Online (Sandbox Code Playgroud)

这是另一个:

app.get('/memo/list', function(req, res, next) {
  console.log("get memos");
  Memo.find({}, function(err, data) {
    if(err) return next(err);
    res.json(data);
  });
});
Run Code Online (Sandbox Code Playgroud)

取自构建在节点上简单便笺簿

这些问题令我困惑:

  1. 究竟做了next/next();什么?如果它不存在会发生什么?
  2. 为什么第二部分next作为参数而第一部分不是?

编辑:

node.js express

64
推荐指数
1
解决办法
3万
查看次数

Passport身份验证回调未通过req和res

这个身份验证工作正常,我得到一个重定向:

server.post(authPostRoute, passport.authenticate(
    'local'
    , { successRedirect: '/', failureRedirect: '/login' }
));     
Run Code Online (Sandbox Code Playgroud)

调用回调后,此身份验证会挂起:

server.post(authPostRoute, passport.authenticate(
    'local'
    , function(){ console.log('Hitting the callback'); console.log(arguments)}
));     
Run Code Online (Sandbox Code Playgroud)

这记录了以下内容:

{ '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 }
Run Code Online (Sandbox Code Playgroud)

但是在整个文档(http://passportjs.org/guide/authenticate/)中,它看起来好像是通过req和res传递的,但显然不是.然后是调用回调的代码:

node_modules \护照\ LIB \中间件\ authenticate.js

  strategy.success = function(user, info) {
    if (callback) {
      return callback(null, user, info);
    }
Run Code Online (Sandbox Code Playgroud)

不传递这些参数.我究竟做错了什么?

authentication node.js express passport.js

9
推荐指数
1
解决办法
2万
查看次数

标签 统计

express ×3

node.js ×3

passport.js ×2

authentication ×1