javascript - passport.authenticate 不是函数

标签 javascript node.js passport.js

你好,我是 NodeJs 的新手,我一直在关注本教程 http://code.tutsplus.com/tutorials/authenticating-nodejs-applications-with-passport--cms-21619创建具有身份验证的应用程序。 我试图遵循教程中的所有结构和代码(代码在 github https://github.com/tutsplus/passport-mongo 上)但是当我在浏览器中打开我的应用程序时 我得到错误这个错误

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;
}

可能问题就在那里,也许路由在某些版本的 express 中发生了变化,但我无法弄清楚问题是什么。 你能帮我吗?

最佳答案

我遇到了同样的问题。看看 app.js。必须有:

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

关于javascript - passport.authenticate 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36812676/

相关文章:

node.js - 将 Passport 和 OAuth 与 connect-redis 结合使用

javascript - 根据屏幕高度选择下拉高度

javascript - IE 8 的 window.open() 方法 - 强制窗口在选项卡之外打开

javascript - 如何在 Visual Studio 2013 中 'do'(并使用)HTML 包(来自 Web Essentials)

node.js - passportjs,mocha,超测 ECONNREFUSED

node.js - 为什么每次关闭浏览器时我的 session 都会过期?

javascript - 在浏览器上缓存图像

node.js - EJS和WebStorm,错误错误

javascript - 从数组中删除了错误的项目

node.js - 需要 ('os' ).tmpdir() 返回 2 个不同的结果