您好我是NodeJ的新手,我一直在关注本教程http://code.tutsplus.com/tutorials/authenticating-nodejs-applications-with-passport--cms-21619来创建一个带有身份验证的应用。 我试图遵循教程中的所有结构和代码(代码在github https://github.com/tutsplus/passport-mongo上)但是当我在浏览器中打开我的应用程序时 我收到错误此错误
TypeError:passport.authenticate不是函数 在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;
}
可能问题就在那里,也许某些版本的快递路由发生了变化,但我无法弄清楚问题是什么。 你能帮帮忙吗?
答案 0 :(得分:7)
我有同样的问题。看看app.js.必须有:
var routes = require('./routes/index')(passport);
答案 1 :(得分:-2)
您刚将括号放在错误的位置。 它应该是
router.post('/login', passport.authenticate('login'), {
successRedirect: '/home',
failureRedirect: '/',
failureFlash : true
});