Get request object in Passport strategy callback

前端 未结 3 1031
别跟我提以往
别跟我提以往 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.

    0 讨论(0)
  • 2020-12-09 01:54

    There's a passReqToCallback option, see the bottom of this page for details: http://passportjs.org/guide/authorize/

    0 讨论(0)
  • 2020-12-09 01:56

    Try this.

    exports.facebookStrategy = new FacebookStrategy({
            clientID: '.....',
            clientSecret: '...',
            callbackURL: 'http://localhost:3000/auth/facebook/callback',
            passReqToCallback: true
        },function(req,accessToken,refreshToken,profile,done){
            User.findOne({
                    'facebook.id' : profile.id
                },function(err,user){
                if(err){
                    done(err);
                }
                if(user){
                    req.login(user,function(err){
                        if(err){
                            return next(err);
                        }
                        return done(null,user);
                    });
                }else{
                    var newUser = new User();
                    newUser.facebook.id = profile.id;
                    newUser.facebook.name = profile.displayName;
                    newUser.facebook.token = profile.token;
                    newUser.save(function(err){
                        if(err){
                            throw(err);
                        }
                        req.login(newUser,function(err){
                            if(err){
                                return next(err);
                            }
                            return done(null,newUser);
                        });
                    });
                }
            });
        }
    );
    

    User is a mongoose model, i save the user in DB.

    0 讨论(0)
提交回复
热议问题