req.session.passport为空,反序列化用户未调用 – ExpressJS,护照

我在使用Passport / ExpressJS中的会话时遇到问题。

当我loginreq.session ,我可以看到护照是{}

 { cookie: { path: '/', _expires: Mon Sep 29 2014 19:37:16 GMT-0300 (BRT), originalMaxAge: 3594522, httpOnly: true, secure: true }, passport: {} } 

另外,我正在使用Facebook进行身份validation,并且没有调用passport.deserializeUser 。 这是我的代码:

  passport.use(new FacebookStrategy({ // pull in our app id and secret from our auth.js file clientID : configAuth.facebookAuth.clientID, clientSecret : configAuth.facebookAuth.clientSecret, callbackURL : configAuth.facebookAuth.callbackURL }, // facebook will send back the token and profile function(token, refreshToken, profile, done) { // asynchronous process.nextTick(function() { console.log("profile " + profile.id); console.log("profile.name "+profile.name.givenName) // find the user in the database based on their facebook id User.findOne({ 'facebook.id' : profile.id }, function(err, user) { // if there is an error, stop everything and return that // ie an error connecting to the database if (err) return done(err); // if the user is found, then log them in if (user) { //returning undefined //console.log(user.name + " " + user.email); return done(null, user); // user found, return that user } else { // if there is no user found with that facebook id, create them var newUser = new User(); // set all of the facebook information in our user model newUser.facebook.id = profile.id; // set the users facebook id newUser.facebook.token = token; // we will save the token that facebook provides to the user newUser.facebook.name = profile.name.givenName + ' ' + profile.name.familyName; // look at the passport user profile to see how names are returned newUser.facebook.email = profile.emails[0].value; // facebook can return multiple emails so we'll take the first // save our user to the database newUser.save(function(err) { if (err) throw err; // if successful, return the new user return done(null, newUser); }); } }); }); })); // used to serialize the user for the session passport.serializeUser(function(user, done) { console.log("serialize"); done(null, user.id); }); // used to deserialize the user passport.deserializeUser(function(id, done) { console.log("deserialize"); User.findById(id, function(err, user) { console.log(user); done(err, user); }); }); 

我试着将两种方法都移到开头,但没有任何区别。 passport.serializeUser被称为很好。

这是我启动passport

 app.use (cookieParser()); app.use(session({ secret: 'appsecret', saveUninitialized: true, cookie: { secure: true, maxAge: new Date(Date.now() + 3600000) }, key:'connect.sid' })); app.use(passport.initialize()); app.use(passport.session()); 

这是我的route

 // ===================================== // FACEBOOK ROUTES ===================== // ===================================== // route for facebook authentication and login app.get('/auth/facebook', passport.authenticate('facebook', { scope : 'email' })); // handle the callback after facebook has authenticated the user app.get('/auth/facebook/callback', passport.authenticate('facebook', { successRedirect : '/home', failureRedirect : '/login' })); 

有人可以帮我弄这个吗?

谢谢!

背景

对于我来说deserializeUser没有被调用的问题,空的护照对象是跨域的问题。 当我从本地主机发送一个Ajax请求:4200到本地主机:1000个cookie和会话不适用于我的Ember.js客户端。 然而,邮递员延期收到正确的答复。 护照对象也正确地填充了数据。 User.deserializeUser被调用正确。

为了解决这个问题,我必须设置正确的服务器端和客户端HTTP头。 服务器端(node.js +快递+护照):

 # The important part. Must go AFTER the express session is initialized app.use passport.initialize() app.use passport.session() # Enable CORS # X-Requested-With, X-AUTHENTICATION, X-IP # https://stackoverflow.com/a/18290875/2166409 app.use (req, res, next) -> res.header 'Access-Control-Allow-Origin', 'http://localhost:4200' res.header 'Access-Control-Allow-Headers', 'Origin, X-Requested-With, X-AUTHENTICATION, X-IP, Content-Type, Accept' res.header 'Access-Control-Allow-Credentials', true res.header 'Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS' next() app.use '/api/v1', router 

客户端:

如果使用jQuery,请将其添加到$ .ajax选项中:

 xhrFields: { withCredentials:true } 

然后一切按预期工作:

 DESERIALIZE USER { id: '547fabf22a098ade53e6e48e', name: 'test', firstName: 'test', email: 'test@wp.pl' } LOGIN SUCCESS SESSION { cookie: { path: '/', _expires: Thu Dec 04 2014 20:26:52 GMT+0100 (CET), originalMaxAge: 3597205, httpOnly: false }, passport: { user: { id: '547fabf22a098ade53e6e48e', name: 'test', firstName: 'test', email: 'test@wp.pl' } } } 

这些答案帮助了我: 关于Ember.js , jQuery.ajax 。

对我来说,根本原因是快速会话中的Cookie安全值。

 app.use(require('express-session')({ secret: 'crackalackin', resave: true, saveUninitialized: true, cookie : { secure : false, maxAge : (4 * 60 * 60 * 1000) }, // 4 hours })); 

在我的本地机器上,我没有运行HTTPs,因此当cookie.secure为true时,会话和用户对象是空的。 当在开发模式和安全是错误的,它工作正常。

我的解决scheme是连接我的网站的不同页面是一致的。

在“www”前加上链接的url 同时validation哪里没有“www”。 链接到该前缀页面时将开始一个新的会话。 您可以使用cookie查看器来查看,看看是否有多个cookie用于这两个选项。