Max*_*ogg 16 facebook node.js passport.js
使用Node,Express和Passport.
好的,我和我的团队正在为双面市场类型的应用程序构建REST API.我们已经为电子邮件和密码登录设置了基本的LocalStrategy.
我们希望API用户代理不可知,因此我们可以通过Web,Android或iOS使用API.
但是我们感到困惑的是FB登录流程.问题是,在Passportjs的幕后究竟发生了什么.我们已经研究了'passport-facebook'和'passport-facebook-token'策略,并且无法真正决定使用哪种策略.
这是我目前对流程的理解:


如果这是正确的,我最好让客户端从FB获取access_token然后发送给我,或者让FB通过重定向和回调URL处理它?
护照令牌:
passport.use('facebook-token', new FacebookTokenStrategy( {
clientID: 'xxx',
clientSecret: 'xxx'
}, function(accessToken, refreshToken, profile, done) {
// asynchronous
//console.log("into passport auth");
process.nextTick(function() {
User.findOne({'facebook.id': profile.id}, function(error, user) {
console.log("user is " + JSON.stringify(user));
console.log("profile is " + JSON.stringify(profile));
//do user creation stuff etc.
return done(error, user);
});
});
}));
authRouter.post('/facebook', passport.authenticate('facebook-token'), function (req, res) {
console.log("into controller");
if (req.user){
//log the user in since they successfully authenticated with facebook.
req.login(user);
res.status(200).end();
} else {
res.status(401).end();
}
});
Run Code Online (Sandbox Code Playgroud)
护照Facebook的:
passport.use('facebook', new FacebookStrategy( {
callbackURL: "http://75.128.65.176:8080/auth/facebook/callback",
clientID: 'xxx',
clientSecret: 'xxx'
}, function(accessToken, refreshToken, profile, done) {
// asynchronous
//console.log("into passport auth");
process.nextTick(function() {
User.findOne({'facebook.id': profile.id}, function(error, user) {
console.log("user is " + JSON.stringify(user));
console.log("profile is " + JSON.stringify(profile));
//do user creation stuff etc.
return done(error, user);
});
});
}));
// Redirect the user to Facebook for authentication. When complete,
// Facebook will redirect the user back to the application at
// /auth/facebook/callback
authRouter.get('/facebook', passport.authenticate('facebook'));
// Facebook will redirect the user to this URL after approval. Finish the
// authentication process by attempting to obtain an access token. If
// access was granted, the user will be logged in. Otherwise,
// authentication has failed.
authRouter.get('/facebook/callback',
passport.authenticate('facebook', { successRedirect: '/',
failureRedirect: '/login' }));
Run Code Online (Sandbox Code Playgroud)
关于此流程实际如何运作的任何细节/详细说明将非常感激!
使用本机 iOS 和 Android Facebook SDK 时,客户端 Facebook 重定向很不方便 - 因为它们有时会重定向到用户安装的 Facebook 应用程序。因此,如果您想要一个真正通用的 API,那么您应该选择passport-facebook-token.
小智 5
验证显示的 2 个流程是否正确。
是的,他们是正确的。
问:我有一个 API 列表。我如何使用护照-脸书策略保护他们
你有几个选项:
Facebook tokenFacebook token用户信息Facebook token每次调用 API 时都会发送Facebook token有关如何在Facebook token 此处进行验证的更多信息。
JWT在检索到 Facebook 用户信息后返回一个JWT每次调用 API 时都会发送JWT这样,服务器就不必向 Facebook 发送请求来验证Facebook token. 更多信息在这里。
问:如果我使用的是passport-facebook-token,我如何告诉用户去登录facebook
您/api/auth/facebook只接受Facebook token并返回相应的HTTP code. 因此,要求用户去 Facebook 登录是客户的工作。
有关如何在此处手动创建 Facebook 登录的更多信息。
| 归档时间: |
|
| 查看次数: |
4850 次 |
| 最近记录: |