当我使用passport JS成功登录时出现此错误。登录后尝试重定向到主页。
执行此操作的代码:
app.post('/login',
passport.authenticate('local', {failureRedirect: '/login' }),
function(req, res) {
res.redirect('/');
});
完整错误:
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (http.js:644:11)
我错过了什么吗?不知道为什么会发生这个错误。我仍然可以使用该应用程序,我只是不想出现错误。
您正在重定向用户,因此 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);
}
});
});
}
));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)