您正在重定向用户,因此 serializeUser 函数被调用了两次。而在
passport.use(new FacebookStrategy({
...
一定要添加这个else否则它会被调用两次,从而发送两次标头并导致错误。尝试这个:
passport.use(new FacebookStrategy({
...
},
function(accessToken, refreshToken, profile, done) {
// asynchronous verification, for effect...
process.nextTick(function () {
// To keep the example simple, the user's Facebook profile is returned to
// represent the logged-in user. In a typical application, you would want
// to associate the Facebook account with a user record in your database,
// and return that user instead.
User.findByFacebookId({facebookId: profile.id}, function(err, user) {
if (err) { return done(err); }
if (!user) {
//create user User.create...
return done(null, createdUser);
} else { //add this else
return done(null, user);
}
});
});
}
));