2013-07-30 67 views
2

我必須在這裏俯瞰的東西。我正在使用passportjs的Facebook策略來驗證用戶身份。這是與2個請求/ [路由執行]完成:passportjs臉譜通過要求回電

//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構造「passReqToCallback」,它發送的最新要求回驗證回調,這是沒有用的,我(我想第一request.body)

接下來的事情提供一個選項,看着一個合理的路徑,是使用了「狀態」選項,如在https://github.com/jaredhanson/passport-oauth/blob/master/lib/passport-oauth/strategies/oauth2.js#L169

但這些值不可對getOAuthAccessToken回調https://github.com/jaredhanson/passport-oauth/blob/master/lib/passport-oauth/strategies/oauth2.js#L124

我現在的選擇是添加OAuth2Strategy內一個額外的變量.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

你的生活救星:)) –