Par*_*ner 8 oauth node.js express passport.js
我正在使用passport.js,如果我可以将facebook id链接到登录用户的帐户,我就会受伤.像这样的东西:
passport.use( new FacebookStrategy({
consumerKey: ---
consumerSecret: ---
callbackURL: "http://mycallback"
},
function(token, tokenSecret, profile, done) {
if (user is logged in)
user = User.addfacebookId(user, profile.id)
done(user);
}
}
));
Run Code Online (Sandbox Code Playgroud)
Jar*_*son 18
有几种方法可以解决这个问题,但我认为最直接的方法之一是使用该passReqToCallback选项.启用后,req成为验证回调的第一个参数,然后您可以检查是否req.user存在,这意味着用户已经登录.此时,您可以将用户与Facebook个人资料详细信息关联并提供相同的信息.用户实例到完成回调.如果req.user不存在,只需像往常一样处理它.
例如:
passport.use(new FacebookStrategy({
clientID: ---
clientSecret: ---
callbackURL: "http://mycallback"
passReqToCallback: true
},
function(req, accessToken, refreshToken, profile, done) {
if (req.user)
// user is already logged in. link facebook profile to the user
done(req.user);
} else {
// not logged in. find or create the user based on facebook profile
}
}
));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2468 次 |
| 最近记录: |