“TypeError:req.flash不是函数”使用带有nodejs,用户名和密码auth的护照

时间:2016-03-18 14:46:38

标签: javascript node.js passport.js

运行时输入控制台输出并输入错误的密码..

info: Listening on 127.0.0.1:3000
debug: GET /
debug: Incorrect password
/home/bob/git/authenticate-nodejs-prototype/node_modules/mongodb/lib/utils.js:98
    process.nextTick(function() { throw err; });
                                  ^

TypeError: req.flash is not a function
    at allFailed (/home/bob/git/authenticate-nodejs-prototype/node_modules/passport/lib/middleware/authenticate.js:118:15)

这是实际的代码。可能导致这种情况的任何想法? ..

var express = require('express'),
    app = express(),
    http = require('http').Server(app),
    winston = require('winston'),
    passport = require('passport'),
    LocalStrategy = require('passport-local').Strategy,
    ipaddress = '127.0.0.1',
    port = 3000,
    MongoClient = require('mongodb').MongoClient,
    ObjectId = require('mongodb').ObjectID,
    assert = require('assert'),
    mongoUrl = 'mongodb://' + ipaddress + ':27017/authenticate-nodejs-prototype',
    flash = require('connect-flash');

// during dev
winston.level = 'debug';


/*
 * Database query
 * Searches db for user that matches provided username
 */
var findUser = function (db, id, callback) {
    var cursor = db.collection('userInfo').find({username: id.username});
    var result = [];
    cursor.each(function (err, doc) {
        assert.equal(err, null);
        if (doc !== null) {
            result.push(doc);
        } else {
            callback(result);
        }
    });
};


// configure passport to use username and password authentication
passport.use(new LocalStrategy(
  function(username, password, done) {
        MongoClient.connect(mongoUrl, function (err, db) {
            assert.equal(null, err);
            findUser(db, {username:username, password:password}, function (result) {
                db.close();

                if (err) {
                    return done(err);
                }else if (result === []) {
                    winston.debug('Incorrect username');
                return done(null, false, { message: 'Incorrect username.' });
              }else if (password !== result.password) {
                    winston.debug('Incorrect password');
                return done(null, false, { message: 'Incorrect password.' }); // this is the line that causes error
              }
              return done(null, result);
            });
        });
  }
));
passport.serializeUser(function(user, done) {
  return done(null, user);
});
passport.deserializeUser(function(id, done) {
    MongoClient.connect(mongoUrl, function (err, db) {
        assert.equal(null, err);
        findUser(db, id, function (result) {
            db.close();
            return done(null, result);
        });
    });
});

app.configure(function() {
  app.use(express.static('public'));
  app.use(express.cookieParser());
  app.use(express.bodyParser());
  app.use(express.session({ secret: 'keyboard cat' }));
  app.use(passport.initialize());
  app.use(passport.session());
  app.use(app.router);
  app.use(flash());
});

/*
 * setup endpoints
  */
app.get('/', function(req, res){
    winston.debug('GET /');
  res.sendfile('views/index.html');
});
app.get('/success', function(req, res){
    winston.debug('GET /success');
  res.sendfile('views/success.html');
});
app.post('/login',
  passport.authenticate('local', { successRedirect: '/success',
                                   failureRedirect: '/',
                                   failureFlash: true })
);

// start server
http.listen(port, ipaddress, function(){
  winston.info('Listening on ' + ipaddress + ':' + port);
});

1 个答案:

答案 0 :(得分:4)

您需要添加flash来表达,以便它可以用作中间件。

var flash = require('connect-flash');
...
app.use(flash());

编辑:原因是Flash实际上没有内置到Passport或Express中,它是由connect-flash提供的单独包。所以你需要用npm安装它,然后如上所示包含它。