简体   繁体   English

如何将附加参数传递给 passport.authenticate

[英]How to pass an additional parameter to passport.authenticate

(I already do fbStrategy.passReqToCallback = true ) I am riffing off https://scotch.io/tutorials/easy-node-authentication-linking-all-accounts-together but want to use this social authentication service for multiple apps, ex: the one that controls the heating system, the one that turns on the sprinklers etc. (我已经做fbStrategy.passReqToCallback = true )我正在重复 https://scotch.io/tutorials/easy-node-authentication-linking-all-accounts-together但想将此社交身份验证服务用于多个应用程序,例如:控制供暖系统的那个,打开洒水器的那个等等。

Basically if one of these apps checks with the server and doesn't have a correct token it get redirected to this social authentication service (social-auth).基本上,如果其中一个应用程序与服务器核对并且没有正确的令牌,它就会被重定向到这个社交身份验证服务 (social-auth)。 When the user presses on of the social login buttons it grabs the parameter of what app its arriving from and adds it as a parameter for /auth/facebook/:appid当用户按下社交登录按钮时,它会获取它来自哪个应用程序的参数,并将其添加为/auth/facebook/:appid的参数

    // send to facebook to do the authentication
    app.get('/auth/facebook/:appId', function(req,res,next){
        passport.authenticate(
            'facebook', { scope : 'email' }
        )(req,res,next);
    });

req of req,res,next is the serialized user record. req req,res,next的 req 是序列化的用户记录。 At this point social-auth doesn't know who the user is.此时 social-auth 不知道用户是谁。

fbStrategy.passReqToCallback = true;  
passport.use(new FacebookStrategy(fbStrategy,
    function(req, token, refreshToken, profile, done) {
        var email = (profile.emails[0].value || '').toLowerCase()     
        process.nextTick(function() {...

Once authorization is complete I want to redirect back to the calling app and I need the :appId param to ride along so I can go back to the right site.授权完成后,我想重定向回调用应用程序,我需要:appId参数,以便我可以 go 返回正确的站点。

Now generally it would work if I just made a variable like currentAppId accessible to the various social stategies but If you happened to have multiple people authenticating at the same time then you conceivably have a user return to the wrong app, or some other users app.现在,如果我只是让各种社交策略都可以访问currentAppId之类的变量,通常它会起作用,但是如果您碰巧有多个人同时进行身份验证,那么可以想象,您可能会让用户返回到错误的应用程序或其他一些用户应用程序。 That's why I need appId to travel as param to passport.authenticate .这就是为什么我需要appId作为参数旅行到passport.authenticate Where should I be looking to figure out how.我应该在哪里寻找弄清楚如何。 Could I wrap passport.authenticate or otherwise modify things?我可以包装passport.authenticate或以其他方式修改东西吗?

You can use the request itself to transfer some additional parameters from and to the strategy function. 您可以使用请求本身从策略功能传输一些其他参数。 In the following example the two parameters _toParam and _fromParam are used for this concern. 在以下示例中,两个参数_toParam_fromParam用于此问题。

app.get('/auth/facebook/:appId', function(req,res,next){
    req._toParam = 'Hello';
    passport.authenticate(
        'facebook', { scope : 'email' }
    )(req,res,next);
})


fbStrategy.passReqToCallback = true;  
passport.use(new FacebookStrategy(fbStrategy,
    function(req, token, refreshToken, profile, done) {
        console.log(req._toParam);
        req._fromParam = 'Hello 2';
        var email = (profile.emails[0].value || '').toLowerCase()     
    process.nextTick(function() {...

If you need to store the id for further requests, you could use a map with the socket of the current request as key. 如果您需要存储id以进一步请求,您可以使用当前请求的套接字作为键的映射。

In my case, due to the authentication flow, to set up a variable in req was not working.在我的例子中,由于身份验证流程,无法在req中设置变量。

The second option, set passReqToCallBack: true was giving me many issues, so it was not an option either.第二个选项,set passReqToCallBack: true给我带来了很多问题,所以它也不是一个选项。 More information can be found here .可以在此处找到更多信息

The solution came to me following the answer that Dhiraj posted in thread above, I did set a single string in the state and I was able to use the string on the callbacks.根据Dhiraj在上面的线程中发布的答案,我找到了解决方案,我确实在state中设置了一个字符串,并且我能够在回调中使用该字符串。

 login: (req, res, next) => passport.authenticate('google', { scope: ['profile', 'email'], state: req.query.username }, () => next())(req, res, next), onSuccess: async(req, res) => { console.log(req.query.state) },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM