gpt4 book ai didi

javascript - Node.js Express Passport.js - 数据不会序列化

转载 作者:行者123 更新时间:2023-11-30 17:48:40 25 4
gpt4 key购买 nike

/**
* 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)

最佳答案

我认为您缺少建立登录 session 所需的 passport.serializeUser 函数。来自passport docs :

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

passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});

尝试在 passport.use(new LocalStrategy(...

同时检查这个相关问题:Node.js user authentication using passport

关于javascript - Node.js Express Passport.js - 数据不会序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19570519/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com