2012-11-26 1 views
9

여권 JS로 성공적으로 로그인하면이 오류가 발생합니다. .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) 

내가 뭔가를 놓치고 나는 그것을하지

코드 로그인하면 홈 페이지로 리디렉션하려고? 이 오류가 발생한 이유를 잘 모릅니다. 나는 여전히 응용 프로그램을 사용할 수있어, 난 그냥 오류 싶지 않아.

답변

15

serializeUser 함수가 두 번 호출되도록 사용자를 리디렉션합니다. 그리고

passport.use(new FacebookStrategy({ 
... 

이 다른 을 추가해야하거나, 따라서 두 번 헤더를 전송하고 오류의 원인이, 두 번 호출됩니다. 이것을 시도하십시오 :

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); 
     } 
    }); 
    }); 
} 
)); 
+4

감사합니다. done()을 두 번 호출했습니다. – wesbos

+2

감사합니다. 나는 당신이 'else'가 필요하다는 것을 깨닫기 전에 많은 머리를 긁었다. 멋진 사용자 이름과 아바타. – electrichead

1

PassportJS guide에 따르면, 미들웨어가 모든 리디렉션을하도록 가정합니다.

app.post('/login', passport.authenticate('local', { successRedirect: '/', 
               failureRedirect: '/login' })); 

내 생각 엔 미들웨어는 다음 방법을 시도하고 (말아야 때 next를 호출)는 위의 예에있는 것처럼 익스프레스 'res.redirect 메소드를 호출하지만, 그 구현에 오류가된다는 것이다 res.redirect을 다시 호출하면 HTTP 프로토콜에서 한 번만 클라이언트에 응답을 보낼 수 있기 때문에 오류가 발생합니다.

관련 문제