Node.js passportjs facebook通过回调请求

Node.js passportjs facebook通过回调请求,node.js,facebook,authentication,passport.js,Node.js,Facebook,Authentication,Passport.js,我一定是忽略了什么。我正在使用passportjs的Facebook策略对用户进行身份验证。这是通过2个请求/[路由处理程序]完成的: //one to initiate the the auth: init: function (req, res, next) { passport.authenticate('facebook', { callbackURL: URL + '/facebook/callback', state: req.body //a

我一定是忽略了什么。我正在使用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”提供了一个选项,它将最新的请求发送回verify回调,这对我没有用处(我想要第一个request.body)

下一个看似可行的方法是使用“状态”选项,如中所示

但是这些值在getOAuthAccessToken回调上不可用


我当前的选择是在OAuth2Strategy.prototype.authenticate()函数中添加一个额外的变量,该函数在第一个函数中设置,并原封不动地传递回回调函数,但我无法想象这是一种方法。

根据您的描述,最好的方法可能取决于您的应用程序,但是这里是对
init
callback
中间件的快速修改:

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
回调中的变异状态可能不可取,因此如果您根据原始身体修改任何内容,请小心。

U r life savior:)