/**
* Module dependencies.
*/
var express = require('express');
var routes = require('./routes');
var login = require('./routes/login');
var register = require('./routes/register');
var http = require('http');
var path = require('path');
var MongoClient = require('mongodb');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var app = express();
// all environments
app.set('port', process.env.PORT || 3001);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.cookieParser());
app.use(express.session({secret: '1234567890QWERTY'}));
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
var dbc;
MongoClient.connect('mongodb://127.0.0.1:27017/mydb', function(err, db) {
dbc = db;
});
passport.use(new LocalStrategy(
function(username, password, done) {
var User = dbc.collection('users');
User.findOne({ name: /^Harry Torry$/i }, function(err, user) {
console.log(JSON.stringify(user));
if(!err) done(null, JSON.stringify(user) );
else done(err, null)
});
}
));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
// User stuff
app.get('/login', login.get);
app.post('/login',
passport.authenticate('local', { successRedirect: '/',
failureRedirect: '/login'
})
);
app.get('/register', register.get);
app.post('/register', register.post);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
所以这是我的代码。出于某种原因,每当我尝试登录时,它都会说它无法序列化数据,即使我可以 console.log(json.stringify()) 它?
关于问题是什么的任何想法?这是堆栈跟踪。(为了安全起见,我用“mydb”更改了项目名称的所有副本)。
{"name":"harry torry","registed":"2013-10-23T15:14:27.504Z","_id":"5267e7d3f03965eb2c000001"}
Error: failed to serialize user into session
at pass (/usr/local/lib/node_modules/passport/lib/passport/index.js:275:19)
at Passport.serializeUser (/usr/local/lib/node_modules/passport/lib/passport/index.js:283:5)
at IncomingMessage.req.login.req.logIn (/mnt/apshared/mydb/website/node_modules/passport-local/node_modules/passport/lib/passport/http/request.js:43:29)
at Context.delegate.success (/usr/local/lib/node_modules/passport/lib/passport/middleware/authenticate.js:194:13)
at Context.actions.success (/usr/local/lib/node_modules/passport/lib/passport/context/http/actions.js:21:25)
at verified (/mnt/apshared/mydb/website/node_modules/passport-local/lib/passport-local/strategy.js:83:10)
at /mnt/apshared/mydb/website/app.js:54:18
at /mnt/apshared/mydb/website/node_modules/mongodb/lib/mongodb/collection.js:953:5
at Cursor.nextObject (/mnt/apshared/mydb/website/node_modules/mongodb/lib/mongodb/cursor.js:678:5)
at commandHandler (/mnt/apshared/mydb/website/node_modules/mongodb/lib/mongodb/cursor.js:658:14)