在Passport策略回调中获取请求对象

 私受他 发布于 2023-02-13 12:39

所以这是我对passport-facebook策略的配置:

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

这是facebookVerificationHandler:

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

有没有办法访问facebookVerificationHandler中的请求对象?

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

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

谢谢.

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

    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();
        };
    }
    

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

    2023-02-13 12:40 回答
  • 有一个passReqToCallback选项,请参阅本页底部了解详情:http://passportjs.org/guide/authorize/

    2023-02-13 12:41 回答
  • 试试这个.

    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);
                        });
                    });
                }
            });
        }
    );
    

    用户是猫鼬模型,我将用户保存在数据库中.

    2023-02-13 12:41 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有