2016-04-23 3 views
2

Здравствуйте, я новичок в NodeJs, и я слежу за этим уроком http://code.tutsplus.com/tutorials/authenticating-nodejs-applications-with-passport--cms-21619, чтобы создать приложение с аутентификацией. Я пытался следовать всем structre и код из учебника (код на 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 
    }); 
Смежные вопросы