Nodejs Passport身份验证错误 - 无法为undefined调用方法身份验证

时间:2015-01-10 15:01:26

标签: javascript node.js authentication express passport.js

我在尝试使用Node.js和Passport.js注册时尝试对用户进行身份验证,但每当我调用authenticate方法时,我都会收到错误并说错误:

TypeError: Cannot call method 'authenticate' of undefined
   at /Users/MarcellusPowell/Desktop/Stake/Code/routes/routes.js:53:13
   at Layer.handle [as handle_request] (/Users/MarcellusPowell/Desktop/Stake/Code/node_modules/express/lib/router/layer.js:76:5)
   at next (/Users/MarcellusPowell/Desktop/Stake/Code/node_modules/express/lib/router/route.js:100:13)
   at next (/Users/MarcellusPowell/Desktop/Stake/Code/node_modules/express/lib/router/route.js:94:14)
   at Route.dispatch (/Users/MarcellusPowell/Desktop/Stake/Code/node_modules/express/lib/router/route.js:81:3)
   at Layer.handle [as handle_request] (/Users/MarcellusPowell/Desktop/Stake/Code/node_modules/express/lib/router/layer.js:76:5)
   at /Users/MarcellusPowell/Desktop/Stake/Code/node_modules/express/lib/router/index.js:227:24
   at Function.proto.process_params (/Users/MarcellusPowell/Desktop/Stake/Code/node_modules/express/lib/router/index.js:305:12)
   at /Users/MarcellusPowell/Desktop/Stake/Code/node_modules/express/lib/router/index.js:221:12
   at Function.match_layer (/Users/MarcellusPowell/Desktop/Stake/Code/node_modules/express/lib/router/index.js:288:3

这是我的 app.js 文件:

var express = require('express');
var morgan = require('morgan');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var methodOverride = require('method-override');
var staticFavicon = require('static-favicon');
var session = require('express-session');
var passport = require('passport');
var path = require('path');
var fs = require('fs');
var http = require('http');
var app = express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(morgan('dev'));
app.use(bodyParser());
app.use(cookieParser());
//app.use(express.json());
//app.use(express.urlencoded());
app.use(methodOverride());
//app.use(staticFavicon(path.join(__dirname + '/public/images/favicon.ico')));
app.use(session({
  secret: 'moneymaking',
  cookie: {
    maxAge: 86400000
  }
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(__dirname, '/public'));

require('./routes/routes')(app, fs);

http.createServer(app).listen(app.get('port'), function() {
  console.log('Express server listening on port ' + app.get('port'));
});

我在我的routes.js中调用这个方法:



var fs = require ('fs');
var mysql = require('mysql');
var passport = require('passport');

var pool = mysql.createPool({
	host		: '',
	user 		: '',
	password	: '',
	port 		: '',
	database	: ''
})


module.exports = function(app, fs, passport){
	
	app.route('/')

		.get(function(req, res, next){
			res.redirect("/home");
		})

	app.route('/home')

		.get(function(req, res, next){
			res.render('index', {title: 'home'});
		})


	app.route('/signup')

		.get(function(req, res, next){
			res.render('signup', {title: 'signup', message: req.sessionmessages});

		})

		.post(function(req, res, next){
			
		})

	app.route('/investor')

		.get(function(req, res, next){
			res.render('investor', {title: 'signup', message: req.sessionmessages});
		})

	app.route('/company')

		.get(function(req, res, next){
			res.render('company', {title: 'Comapny signup', message: req.sessionmessages});
		})

		.post(function(req, res, next){
			passport.authenticate('local-signup', function(err, user, info){
				req.session.messages = info.messages;
				console.log(req.session.messages);
				if(err)
					req.session.messages = "Error creating user"
				console.log(req.session.messages);

				return res.redirect('/home')
			})
		})

	app.route('/about')

		.get(function(req, res, next){
			res.render('about', {title: 'About Us', message: req.sessionmessages});
		})

	//app.route('/:var(login)?')

		/*
		.get(funtction(req, res, next){
			if(req.isAuthenticated())
				res.redirect("/home");
			res.render('login', {title: 'Need a Name', message: req.sessionmessages})
		})
		
		.post.(function(req, res, next)){

		});
		*/

	
}




我在 passport.js 文件中对用户进行序列化和反序列化:

passport.serializeUser(function(user, done) {
  done(null, user);
})

passport.deserializeUser(function(user, done) {
  done(null, user);
})

0 个答案:

没有答案