投掷者; // Node&的未处理'错误'事件护照

时间:2017-01-07 20:17:02

标签: javascript node.js passport.js

我正在尝试使用Passport.js在我的Node应用程序中构建用户身份验证系统。当我尝试注册为新用户时,它似乎工作正常。但是当我尝试登录时,命令提示符给我这个:

  throw er; // Unhandled 'error' event
  ^

TypeError: done is not a function
    at C:\spectray\config\passport.js:55:22
    at C:\spectray\node_modules\mongoose\lib\model.js:3419:16
    at C:\spectray\node_modules\kareem\index.js:212:48
    at C:\spectray\node_modules\kareem\index.js:127:16
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickCallback (internal/process/next_tick.js:98:9)

这是我的passport.js文件:

var passport = require('passport'), LocalStrategy = require('passport-local').Strategy;

var User = require('../app/models/user');

passport.serializeUser(function(user, done) {
    done(null, user.id);
});

passport.deserializeUser(function(id, done) {
    User.findById(id, function(err, user) {
        done(err, user);
    });
});

passport.use('local-signup', new LocalStrategy({

    passReqToCallback : true
},

    function(req, email, password, done) {

        process.nextTick(function() {

            User.findOne({ 'local.email' : email}, function(err, user) {
                if (err)
                    return done(err);

                if (user) {
                    return done(null, false, req.flash('signupMessage', 'That email is already taken.'));
                } else {

                    var newUser = new User();

                    newUser.local.email = email;
                    newUser.local.password = newUser.generateHash(password);

                    newUser.save(function(err) {
                        if (err)
                            throw err;
                        return done(null, newUser);
                    });
                }
            });
        });
}));

passport.use(new LocalStrategy({

    passReqToCallback : true
},

function(username, password, done) {

    User.findOne({ username: username }, function(err, user) {
        if (err) { return done(err); }
        if (!user) {
            return done(null, false, { message: 'Incorrect username.' });
        }
        if (!user.validPassword(password)) {
            return done(null, false, { message: 'Incorrect password.' });
        }
        return done(null, user);
    });
}
));

这是我的server.js文件:

var express = require('express');
var app = express();
var port = process.env.PORT || 8080;
var mongoose = require('mongoose');
var passport = require('passport');
var morgan = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');
var flash = require('connect-flash');
var db = require('./config/database.js');
var favicon = require('serve-favicon');

app.use(favicon(__dirname + '/public/imgs/favicon.ico'));

mongoose.Promise = global.Promise;

mongoose.connect(db.url);
require('./config/passport');

app.use(morgan('dev'));
app.use(cookieParser());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.set('view engine', 'ejs');

app.use(session({ secret: 'penniesfromheaven', resave: true, saveUninitialized: true }));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());

app.use(express.static(__dirname + '/public'));

require('./app/routes.js')(app, passport);

app.listen(port);
console.log('The magic happens on port ' + port);

我尝试删除我的node_modules文件夹并运行npm install,但仍会显示此错误。我做错了什么?

1 个答案:

答案 0 :(得分:1)

因为您在LocalStrategy构造函数中将passReqToCallback选项设置为true,所以使用以下参数调用回调函数:(req, username, password, done),但您的函数正在接受(username, password, done)这就是为什么参数被分配给错误的参数(以及为什么done不能像错误信息那样起作用)。要解决此问题,请尝试删除passReqToCallback选项,如下所示:

passport.use(new LocalStrategy(function(username, password, done) {

    User.findOne({ username: username }, function(err, user) {
        if (err) { return done(err); }
        if (!user) {
            return done(null, false, { message: 'Incorrect username.' });
        }
        if (!user.validPassword(password)) {
            return done(null, false, { message: 'Incorrect password.' });
        }
        return done(null, user);
    });
}));

或者,如果您稍后决定在LocalStrategy中需要req,则将参数添加到函数中,如下所示:

passport.use(new LocalStrategy({

    passReqToCallback : true
},

function(req, username, password, done) {

    User.findOne({ username: username }, function(err, user) {
        if (err) { return done(err); }
        if (!user) {
            return done(null, false, { message: 'Incorrect username.' });
        }
        if (!user.validPassword(password)) {
            return done(null, false, { message: 'Incorrect password.' });
        }
        return done(null, user);
    });
}
));