Node.js auth:使用 Express 护照
Node.js auth: Using passport with Express
我从 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 是如何获得传递给此回调的请求值的?
换句话说,回调签名应该是这样的:
function (token, tokenSecret, profile, done)
不是这个
function (req, token, tokenSecret, profile, done)
...现在 passport 似乎是标准的 Express 中间件,带有
的签名
module.exports = function(req,res,next){};
但我看不出如何像 Chris 那样使用 Passport 访问 req
变量。我是不是从他的文章中遗漏了什么?
查看教程,试试这个:
passport.use(new LinkedInStrategy({
consumerKey: linkedinConfig.clientId,
consumerSecret: linkedinConfig.clientSecret,
callbackURL: serverBaseUrl + '/auth/linkedin/callback',
passReqToCallback : true
}
LinkedIn 策略继承自 Oauth1 策略,在此 line 解释了如何工作,这应该足够了。希望对你有帮助。
我从 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 是如何获得传递给此回调的请求值的?
换句话说,回调签名应该是这样的:
function (token, tokenSecret, profile, done)
不是这个
function (req, token, tokenSecret, profile, done)
...现在 passport 似乎是标准的 Express 中间件,带有
的签名module.exports = function(req,res,next){};
但我看不出如何像 Chris 那样使用 Passport 访问 req
变量。我是不是从他的文章中遗漏了什么?
查看教程,试试这个:
passport.use(new LinkedInStrategy({
consumerKey: linkedinConfig.clientId,
consumerSecret: linkedinConfig.clientSecret,
callbackURL: serverBaseUrl + '/auth/linkedin/callback',
passReqToCallback : true
}
LinkedIn 策略继承自 Oauth1 策略,在此 line 解释了如何工作,这应该足够了。希望对你有帮助。