【问题标题】:passportjs error callback throwing exceptionpassportjs错误回调抛出异常
【发布时间】:2015-01-06 23:08:21
【问题描述】:

我正在将 LocalStrategy 与 MYSQL(带有 sequelize)一起使用,除非 MYSQL 抛出异常(只是为了测试,我关闭了 MYSQL 服务器)。 return done(error) 回调抛出异常导致服务器崩溃。

这是我的一段代码:

passport.use(new LocalStrategy({usernameField: 'email', passwordField: 'password'},
    function (email, password, done) {
        db.User.find({where: {email: email}}).done(function (error, user) {
            if(error) return done(error);

            if (!user) return done(null, false, {message: 'unknown user'});

            //validate password
            if (user.password != password) {
                return done(null, false, {message: 'invalid password'});
            }
            //all ok
            return done(null, user);
        });
    }
));

还有例外:

TypeError: Property 'next' of object #<Context> is not a function
    at Context.actions.error

我做错了什么?谢谢!

编辑:

    req._passport.instance.authenticate('local', function (err, user, info) {
        if (err) return validator.emit('exception', err);

        if (!user) {
            validator.result.errors.push('Username and password combination not found.');
            validator.emit('response');
        } else {
            req.login(user, function (error) {
                if (error) return validator.emit('exception', error);

                validator.emit('response');
            });
        }
    })(req, res);

【问题讨论】:

  • 您能否展示一下您如何在路线中使用passport.authenticate
  • 我添加了authenticate代码
  • 这看起来相当骇人听闻,可能是您的问题的原因。看看this page,关于“自定义回调”的部分;它使用passport.authenticate 而不是req._passport.instance.authenticate
  • 没关系,这不会导致我认为的问题(尝试过)。

标签: node.js passport.js


【解决方案1】:

好吧,愚蠢的我,我错过了最后的next

req._passport.instance.authenticate('local', function (err, user, info) {
.....
})(req, res, next);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-15
    • 2012-05-24
    • 1970-01-01
    • 2012-08-26
    • 1970-01-01
    相关资源
    最近更新 更多