如何修复TypeError:无法读取未定义的passportjs的属性“authenticate”

时间:2015-04-24 04:49:47

标签: javascript node.js passport.js

我正在进行节点护照登录身份验证,但我正在堆叠此错误,这使我很难运行我的节点服务器

/home/emil/Documents/myapp/app/routes.js:14     app.post('/ login',passport.authenticate('local-login',{                                ^ TypeError:无法读取未定义的属性“authenticate”

我该如何解决?

我的代码: server.js

var express = require('express');
var app = express();
var port = 8080;

var cookieParser = require('cookie-parser');
var session = require('express-session');
var morgan = require('morgan');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var passport = require('passport');

var configDB = require('./config/database.js');
mongoose.connect(configDB.url);

app.use(passport.initialize());
app.use(passport.session());
app.use(morgan('dev'));
app.use(cookieParser());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(session({
secret: 'anystringoftext',
saveUninitialized: true,
resave: true
}));

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

//app.use('/', function(req, res){
//  res.send('Our First Express program!');
//  console.log(req.cookies);
//  console.log('===============');
//  console.log(req.session);
//});

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

app.listen(port);
console.log('Server running on port: ' + port);

routes.js:

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

module.exports = function (app, passport) {
app.get('/', function (req, res) {
    res.render('index.ejs');
});

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

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

app.get('/signup', function (req, res) {
    res.render('signup.ejs', {
        message: req.flash('signupMessage')
    });
});

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

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

app.get('/:username/:password', function (req, res) {
    var newUser = new User();
    newUser.local.username = req.params.username;
    newUser.local.password = req.params.password;
    console.log(newUser.local.username + " " + newUser.local.password);
    newUser.save(function (err) {
        if (err)
            console.log(err);
    });
    res.send("Success!");
})
};

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

passport.js

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

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

module.exports - function(passport) {

passport.serializeUser(function(usser, 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: 'email',
    passwordField: 'password',
    passReqToCallback: true
},
function(req, email, password, done){
    process.nextTick(function(){
        User.findOne({'local.username': email}, function(err, user){
            if(err)
                return done(err);
            if(user){
                return done(null, false, req.flash('signupMessage', 'That account exists'));
            } else {
                var newUser = new User();
                newUser.local.username = email;
                newUser.local.password = password;

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

    });
}));

passport.use('local-login', new LocalStrategy({
    usernameField: 'email',
    passwordField: 'password',
    passReqToCallback: true
},
function(req, email, password, done){
    process.nextTick(function(){
        User.findOne({ 'local.username': email}, function(err, user){
           if(err)
               return done(err);
            if(!user)
                return done(null, false, req.flash('loginMessage', 'No user found'));
            if(user.local.password != password)
                return done(null, false, req.flash('loginMessage', 'invalid password'));
        }
           return done(null, user);  
    })
    })
}
))

1 个答案:

答案 0 :(得分:7)

您尚未将passport传递给路线。 server {js;

中的require('./app/routes.js')(app);应为require('./app/routes.js')(app, passport)