9

当我尝试从页面登录时,出现错误return done(null, false, req.flash('loginMessage', 'User does not exist')); TypeError: req.flash is not a function

下面是我的 server.js 和 passport.js 配置:

server.js:

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var session = require('express-session');
var ejs = require('ejs');
var mongoose = require('mongoose');
var passport = require('passport');
var flash = require('connect-flash');


mongoose.connect('mongodb://localhost:mydb');

require('./config/passport')(passport);
app.use(express.static('public'));
app.set('view engine', 'ejs');
app.set('views', __dirname+'/public/views');
app.use(bodyParser.urlencoded({'extended':'true'}));
app.use(bodyParser.json());
app.use(methodOverride());
app.use(session({secret:'learningpassport',
            resave: true,
            saveUnitialized: true}));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());

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


app.listen(3000);

配置/passport.js:

var LocalStrategy = require('passport-local').Strategy;
var User = require('../app/model/user');
module.exports = function(passport){

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({
    usernameField: 'username',
    passwordField: 'password',
    passReqToCallback: true
},
    function(req,username, password, done){
        process.nextTick(function(){
            User.findOne({'local.username': username}, function(err, user){
                if(err) return done(err);

                if(user)
                    return done(null, false, req.flash('signupMessage','The username already exists' ));
                else{
                    var newUser = new User();
                    newUser.local.username = username;
                    newUser.local.password = newUser.generateHash(password);

                    newUser.save(function(err){
                        if(err) throw done(err);
                        return done(null, newUser);
                    });
                }
            });
        });
    }));
passport.use('local-login', new LocalStrategy({
    usernameField: 'username',
    passwordField: 'password',
    passReqToCallBack: true
},function(req,username, password, done){
    process.nextTick(function(){
     User.findOne({'local.username': username}, function(err,user){
         if(err) return done(err);
         if(!user){
             console.log(req.flash);
             return done(null, false, req.flash('loginMessage', 'User does not exist'));
         }

         if(!user.validPassword(password)){
             return done(null,false, req.flash('loginMessage', 'Password is not correct'));
         }

         return done(null, user);
     });
    });
}));
};

我真的很感谢大家的帮助!!!

登录服务.config.js

'use strict';

angular.module('loginservice').factory('lgservice',function($http){
var myservice = {
    async: function(username, password){
        var promise = $http.post('/login',{
            username: username,
            password: password
        }).then(function(response){
            return response;
        });
        return promise;
    }

};
return myservice;
});

路由.js

module.exports = function(app,passport) {

app.get('/', function (req, res) {
    console.log('/');
    res.render('pages/login', {
        message: req.flash('loginMessage')
    });
});

app.get('/about', function (req, res) {
    console.log('/about');
    res.render('pages/about');
});

app.get('/signup', function (req, res) {
    console.log('sign up');
    res.render('pages/signup', {
        message: req.flash('signupMessage')
    });
});

app.get('/logout', function (req, res) {
    req.logout();
    res.redirect('/');
});

app.get('/home', isLoggedIn, function (req, res) {
    res.render('pages/home', {
        user: req.user
    });
});

app.post('/signup', passport.authenticate('local-signup', {
    successRedirect: '/home',
    failureRedirect: '/signup',
    failureFlash: true
}));

app.post('/login', passport.authenticate('local-login', {
    successRedirect: '/home',
    failureRedirect: '/',
    failureFlash: true
}));
};


funtion isLoggedIn(req,res,next){
if(req.isAuthenticated()){
    return next();
}
res.redirect('/');
}
4

3 回答 3

10

您可能必须app.use(flash());向上移动中间件堆栈,以便在护照之前对其进行处理。

于 2016-07-01T05:13:26.647 回答
2

你写错了

passReqToCallback : true  instead of passReqToCallBack: true
于 2016-07-01T17:00:07.203 回答
0

试试这个。

passport.use('local-login', new LocalStrategy({
    usernameField: 'username',
    passwordField: 'password',
    passReqToCallBack: true
  }, function(username, password, done) {
    process.nextTick(function() {
      User.findOne({
        'local.username': username
      }, function(err, user) {
        if (err) return done(err);
        if (!user) {
          console.log(req.flash);
          return done(null, false, req.flash('loginMessage', 'User does not exist'));
        }

        if (!user.validPassword(password)) {
          return done(null, false, req.flash('loginMessage', 'Password is not correct'));
        }

        return done(null, user);
      });
    });
}))(req,res,next);

希望这个帮助。

于 2016-07-01T05:05:38.457 回答