2016-04-23 259 views
2

你好我是NodeJs中的新成員,我一直在關注本教程http://code.tutsplus.com/tutorials/authenticating-nodejs-applications-with-passport--cms-21619以創建一個具有身份驗證的應用程序。 我試圖按照一切從教程和結構法代碼(代碼是在github https://github.com/tutsplus/passport-mongo),但是當我在瀏覽器中打開 我的應用程序,我得到錯誤這個錯誤passport.authenticate不是函數

TypeError: passport.authenticate is not a function at module.exports (C:\myApp\routes\index.js:24:34)

這是我index.js路由文件

var express = require('express'); 
var router = express.Router(); 
var passport = require('passport'); 

var isAuthenticated = function (req, res, next) { 
    // if user is authenticated in the session, call the next() to call the next request handler 
    // Passport adds this method to request object. A middleware is allowed to add properties to 
    // request and response objects 
    if (req.isAuthenticated()) 
    return next(); 
    // if the user is not authenticated then redirect him to the login page 
    res.redirect('/'); 
} 

module.exports = function(passport){ 

    /* GET login page. */ 
    router.get('/', function(req, res) { 
    // Display the Login page with any flash message, if any 
    res.render('index', { message: req.flash('message') }); 
    }); 

    /* Handle Login POST */ 
    router.post('/login', passport.authenticate('login', { 
    successRedirect: '/home', 
    failureRedirect: '/', 
    failureFlash : true 
    })); 

    /* GET Registration Page */ 
    router.get('/signup', function(req, res){ 
    res.render('register',{message: req.flash('message')}); 
    }); 

    /* Handle Registration POST */ 
    router.post('/signup', passport.authenticate('signup', { 
    successRedirect: '/home', 
    failureRedirect: '/signup', 
    failureFlash : true 
    })); 

    /* GET Home Page */ 
    router.get('/home', isAuthenticated, function(req, res){ 
    res.render('home', { user: req.user }); 
    }); 

    /* Handle Logout */ 
    router.get('/signout', function(req, res) { 
    req.logout(); 
    res.redirect('/'); 
    }); 

    return router; 
} 

可能問題在那裏,也許路由是在某些版本的快速變化,但我不知道是什麼問題。 你能幫助pme嗎?

回答

5

我有同樣的問題。看app.js.必須有:

var routes = require('./routes/index')(passport); 
-1

你剛剛把括號放在錯誤的地方。 它應該是

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