Nic*_*ick 5 node.js express passport-facebook passport.js
我正在使用PassportJS来处理浏览器和移动客户端的FB身份验证.对于网络用户,我使用Passport FacebookStrategy,这是按预期工作的.我还想允许移动客户端访问我的API.我正在尝试使用Passport FacebookTokenStrategy来促进这一点.这似乎与一个小问题有关.当移动客户端向服务器发出GET请求时,使用FacebookTokenStrategy并调用验证回调函数.在验证功能中,我可以看到用户配置文件可用,因此验证成功.但是,在对移动客户端的响应中发回404状态404.我不确定如何正确配置.这就是我目前正在尝试的:
// Web based auth
passport.use(new FacebookStrategy({
clientID: Config.facebook.clientID,
clientSecret: Config.facebook.clientSecret,
callbackURL: "http://localhost/auth/facebook/callback"
},
function(accessToken, refreshToken, profile, done) {
User.findOrCreate(profile, function(err, user){
done(err, user);
});
}
));
// Mobile client auth
passport.use(new FacebookTokenStrategy({
clientID: Config.facebook.clientID,
clientSecret: Config.facebook.clientID
},
function(accessToken, refreshToken, profile, done) {
console.log(profile);
User.findOrCreate(profile, function(err, user){
done(err, user);
});
}
));
// Redirect the user to Facebook for authentication. When complete,
// Facebook will redirect the user back to the application at
// /auth/facebook/callback
exports.fb_auth = passport.authenticate('facebook',{ scope: 'email' });
// 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.
exports.fb_callback = passport.authenticate('facebook', { successRedirect: '/',
failureRedirect: '/login' });
// Mobile Authentication
exports.mobile_fb_auth = passport.authenticate('facebook-token');
Run Code Online (Sandbox Code Playgroud)
我应该提供passport.authenticate('facebook-token'); 还有一些额外的'onsuccess'回调?这在Web客户端的上下文中是有意义的,但我不确定如何使用facebook-token策略来处理它.
我刚刚遇到了同样的问题并且能够解决它。返回 404 是因为中间件在 Express 中的工作方式。您需要传入第三个函数来成功响应。
第三个函数并不总是被调用。仅当前一个中间件成功时才会调用它。
apiRouter.get('/auth/facebook',
// authenticate with facebook-token.
passport.authenticate('facebook-token'),
// if the user didn't successfully authenticate with the above line,
// the below function won't be called
function(req, res){
res.send(200);
});
Run Code Online (Sandbox Code Playgroud)
`
| 归档时间: |
|
| 查看次数: |
940 次 |
| 最近记录: |