Node.js身份验证:使用Express和Express

时间:2015-11-12 04:39:50

标签: javascript node.js express passport.js

我在Scotch.io看到了这篇非常好的文章:

https://scotch.io/tutorials/easy-node-authentication-linking-all-accounts-together

从2014年1月开始,所以它有点老了:)但Chris告诉我们如何在passport.authenticate('linkedin')回调中检查req.user的值,如下所示:

 passport.use(new LinkedInStrategy({
        consumerKey: linkedinConfig.clientId,
        consumerSecret: linkedinConfig.clientSecret,
        callbackURL: serverBaseUrl + '/auth/linkedin/callback'
    },
    function (req, token, tokenSecret, profile, done) {

        if (req.user) {
            var user = req.user;
            user.linkedin.id = profile.id;
            user.linkedin.token = token;

            user.save(function (err) {
                if (err) {
                    done(err);
                }
                else {
                    done(null, user);
                }
            });

        }
        else{
            User.findOne({'linkedin.id': profile.id}, function (err, user) {
                if (err) {
                    done(err);
                }
                else if (user) {
                    done(null, user);
                }
                else {
                    done(null, null);
                }
            });
        }

    }
));

我的问题是 - Chris如何获得传递给此回调的req值?

换句话说,回调签名应该是这样的:

 function (token, tokenSecret, profile, done) 

不是这个

 function (req, token, tokenSecret, profile, done) 

...现在护照似乎是标准的Express中间件,带有

的签名
module.exports = function(req,res,next){};

但我没有看到如何使用Passport以{Chris}的方式访问req变量。我不知道从他的文章中遗漏了什么?

1 个答案:

答案 0 :(得分:1)

查看教程,试试这个:

 passport.use(new LinkedInStrategy({
    consumerKey: linkedinConfig.clientId,
    consumerSecret: linkedinConfig.clientSecret,
    callbackURL: serverBaseUrl + '/auth/linkedin/callback',
    passReqToCallback : true
}

LinkedIn战略继承了Oauth1策略,在此line解释了如何运作,这应该足够了。希望它有所帮助。