我正在使用PassportJS来处理浏览器和移动客户端的FB身份验证。对于web用户,我正在使用Passport FacebookStrategy,并且它正在按预期工作。我还想允许移动客户端访问我的API。我尝试使用Passport FacebookTokenStrategy来实现这一点。这似乎是有效的,但存在一个小问题。当移动客户端向服务器发出GET请求时,将使用FacebookTokenStrategy并调用verify回调函数。在verify函数中,我可以看到用户配置文件可用,因此身份验证已成功。然而,响应给移动客户端的HTTP状态为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');
我是否应该为passport.authenticate('facebook-token');提供一些额外的“onsuccess”回调函数?在Web客户端的上下文中,这是有意义的,但我不确定如何使用facebook-token策略处理这个问题。