在Passport策略回调中获取请求对象 [英] Get request object in Passport strategy callback

查看:118
本文介绍了在Passport策略回调中获取请求对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的护照-facebook"策略配置:

So here is my configuration for passport-facebook strategy:

    passport.use(new FacebookStrategy({
        clientID: ".....",
        clientSecret: ".....",
        callbackURL: "http://localhost:1337/register/facebook/callback",
    },
    facebookVerificationHandler
    ));

这是facebookVerificationHandler:

And here is facebookVerificationHandler:

var facebookVerificationHandler = function (accessToken, refreshToken, profile, done) { 
    process.nextTick(function () {    
        .......
    });    
};

是否可以访问facebookVerificationHandler中的请求对象?

Is there a way to access to the request object in facebookVerificationHandler?

用户已使用LocalStrategy注册到我的网站,但是他们将能够添加其社交帐户并将这些帐户与他们的本地帐户相关联.调用上面的回调时,req.user中已经可以使用当前登录的用户,因此我需要访问req才能将该用户与facebook帐户相关联.

Users are registered to my site with a LocalStrategy but then they will be able to add their social accounts and associate those account with their local accounts. When the callback above is called, the user who is currently logged in is already available in req.user so I need to access req to associate the user with the facebook account.

这是实现它的正确方法还是我应该考虑另一种方法?

Is this the correct way to implement it or should I consider another way of doing it?

谢谢.

推荐答案

因此,我通常在有请求时设置策略,而不是在应用程序启动时设置策略.例如:

For this reason instead of setting up the strategy when the application starts I usually setup the strategy when there is a request. for instance:

app.get(
    '/facebook/login'
    ,passport_setup_strategy()
    ,passport.authenticate()
    ,redirect_home()
);

var isStrategySetup = false;
var passport_setup_strategy = function(){
    return function(req, res, next){
        if(!isStrategySetup){

            passport.use(new FacebookStrategy({
                    clientID: ".....",
                    clientSecret: ".....",
                    callbackURL: "http://localhost:1337/register/facebook/callback",
                },
                function (accessToken, refreshToken, profile, done) { 
                    process.nextTick(function () {    
                        // here you can access 'req'
                        .......
                    });    
                }
            ));

            isStrategySetup = true;

        }

        next();
    };
}

使用此方法,您将可以在验证处理程序中访问该请求.

Using this you will have access to the request in your verification handler.

这篇关于在Passport策略回调中获取请求对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆