2013-07-30 2 views
2

여기에서 뭔가를 놓쳐 야합니다. 나는 passportjs의 Facebook 전략을 사용하여 사용자를 인증합니다.passportjs facebook pass 콜백 요청

//one to initiate the the auth: 
init: function (req, res, next) { 
    passport.authenticate('facebook', { 
     callbackURL: URL + '/facebook/callback', 
     state: req.body //attempting to put some state 
    })(req, res, next) 
} 

//one callback 
callback: function (req, res, next) { 
    passport.authenticate('facebook', { 
     callbackURL: URL + '/facebook/callback' 
    }, 
    function (err, profile, accessToken, refreshToken) { 
     if (err) return next(err) 
     res.send(passedReqBody) 
    })(req, res, next) 
} 

//the verify callback doesn't do much. 
//Application logic is done in route callback handlers 
passport.use(new FacebookStrategy({ 
    clientID: config.facebook.id, 
    clientSecret: config.facebook.secret 
}, 
//When setting passReqToCallback to true, it is set as the first argument 
//to the verify callback. As in: 
//function (req, accessToken, refreshToken, params, profile, done) { 
//But this is the 'callback' request object. I want the 'init' request object. 
function (accessToken, refreshToken, params, profile, done) { 
    //params.state is undefined 
    return done(null, profile, accessToken, refreshToken); 
})); 

내 문제는 내가 처음 함수의 POST 요청 본문이 콜백 경로 처리기에서 노출 할 것입니다 : 이것은이 개 요청/[경로 핸들러]로 이루어집니다. OAuth2Strategy 생성자 나에게 도움이되지 않습니다 확인 콜백 (내가 처음 request.body을 원하는)

다음 일은 다시 최신 요청을 보냅니다 'passReqToCallback'에 사용할 수있는 옵션이 있습니다

그 , 그럴듯한 경로를 보았다 https://github.com/jaredhanson/passport-oauth/blob/master/lib/passport-oauth/strategies/oauth2.js#L169

에서와 같이 '상태'옵션을 사용했다 그러나이 값은 https://github.com/jaredhanson/passport-oauth/blob/master/lib/passport-oauth/strategies/oauth2.js#L124

나의 현재 옵션은 OAuth2Strategy 내부에 별도의 변수를 추가하는 것입니다 getOAuthAccessToken 콜백에 사용할 수 없습니다 .prototype.authenticate() 함수는 첫 번째 함수에서 설정되고 콜백 함수로 변경되지 않은 상태로 전달되지만이 방법이 있다고 생각하지 않습니다. 당신이 무엇을 설명에서

답변

6

, 가장 좋은 방법은 응용 프로그램에 의존 할 수 있지만, 여기 initcallback 미들웨어의 빠른 수정입니다 :

init: function (req, res, next) { 
    // SAVE BODY IN SESSION 
    req.session.initBody = req.body; 

    passport.authenticate('facebook', { 
     callbackURL: URL + '/facebook/callback', 
     state: req.body //attempting to put some state 
    })(req, res, next) 
} 

//one callback 
callback: function (req, res, next) { 
    passport.authenticate('facebook', { 
     callbackURL: URL + '/facebook/callback' 
    }, 
    function (err, profile, accessToken, refreshToken) { 
     if (err) return next(err) 
     // RESTORE BODY FROM SESSION 
     res.send(req.session.initBody); 
     delete req.session.initBody; 
    })(req, res, next) 
} 

원래 요청 본문을 세션에 지속되고있는 것으로 도시 콜백시 복원됩니다. 데이터가 요청/응답주기를 견딜 수 있기를 원한다면이 방법을 사용하는 것이 좋습니다. 그러나 GET 콜백의 상태 변경은 바람직하지 않을 수 있으므로 원래 몸체를 기반으로하는 모든 것을 수정하면주의해야합니다.

+0

U r life savior :)) –