我在使用Passport / ExpressJS中的会话时遇到问题。
当我记录req.session
时,我可以看到护照是{}
:
{ cookie:
{ path: '/',
_expires: Mon Sep 29 2014 19:37:16 GMT-0300 (BRT),
originalMaxAge: 3594522,
httpOnly: true,
secure: true },
passport: {} }
此外,我正在使用Facebook进行身份验证,并且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'
}));
有人可以帮我这个吗?
谢谢!
答案 0 :(得分:8)
对我来说,没有调用deserializeUser和空护照对象的问题是跨域问题。当我从localhost发送ajax请求时:4200到localhost:1000个cookie和会话对我的Ember.js客户端不起作用。但是,Postman扩展收到了正确的答复。护照对象也正确地填充了数据。 User.deserializeUser被正确调用。
要解决此问题,我必须设置正确的服务器端和客户端HTTP标头。 服务器端(node.js + express + passport):
# 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' } } }
这些答案对我有帮助:about Ember.js,jQuery.ajax。
答案 1 :(得分:6)
对我来说,根本原因是快递会话中的cookie安全值。
app.use(require('express-session')({
secret: 'crackalackin',
resave: true,
saveUninitialized: true,
cookie : { secure : false, maxAge : (4 * 60 * 60 * 1000) }, // 4 hours
}));
在我的本地计算机上,我没有在HTTP上运行,因此当cookie.secure为true时,会话和用户对象为空。在开发模式下,安全性为假,它可以正常工作。
答案 2 :(得分:0)
我的解决方案是在链接我网站的不同页面时保持一致。
使用' www。'前缀链接的网址。在验证哪里没有' www。'链接到该前缀页面时将启动一个新会话。 您可以使用Cookie查看器进行检查,看看这两个选项中是否有多个Cookie。