我一定在这里忽略了一些东西。我正在使用 passportjs 的 Facebook 策略来验证用户身份。这是通过 2 个请求/[路由处理程序] 完成的:
//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() 函数中添加一个额外的变量,该变量在第一个函数中设置,并原封不动地传回回调函数,但我无法想象这是去。