Get request object in Passport strategy callback

前端 未结 3 1050
别跟我提以往
别跟我提以往 2020-12-09 01:09

So here is my configuration for passport-facebook strategy:

    passport.use(new FacebookStrategy({
        clientID: \".....\",
        clientSecret: \"....         


        
3条回答
  •  温柔的废话
    2020-12-09 01:51

    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.

提交回复
热议问题