【发布时间】:2014-11-24 09:59:03
【问题描述】:
我在 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'
}));
有人可以帮我解决这个问题吗?
谢谢!
【问题讨论】:
-
您能告诉我们初始化 Passport 的代码吗?人们经常忘记初始化它,或者以错误的顺序执行它。它应该像这样初始化:
app.use(express.session({ secret: 'keyboard cat' })); app.use(passport.initialize()); app.use(passport.session());另外,你能告诉我们认证路径的代码吗? -
已在问题中编辑 :)
-
好的,对我来说一切都很好。愚蠢的问题:您说注销
req.session时passport为空。你在哪里(在代码中)做的?另外,您说passport.serializeUser被调用就好了。,但是当您尝试记录user参数时会发生什么? -
我在app.get('/home')中注销,这是认证成功时调用的路由。用户未定义
-
@LarissaLeite 你解决了吗?
标签: node.js session express passport.js