护照JS - FacebookTokenStrategy返回404

5

我正在使用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策略处理这个问题。


嗨。你找到404错误的原因了吗?我也遇到了同样的问题。 - shanks
你好,我也遇到了类似的问题。 首先,我看到你使用Config.facebook.clientID作为clientID和clientSecret。可能是拼写错误?你是否实现了User.findOrCreate方法? 你是否在app.use(passport.initialize())之后实现了passport.serializeUser和passport.deserializeUser? - jonasonline
顺便说一句...如果你还没有尝试过,我可以推荐编辑器Brackets。它有一个针对Node的很棒的调试器,叫做Theseus。 虽然你遇到了404错误很奇怪...如果是401或500错误会更有意义... - jonasonline
@jonasonline 嘿,有人解决了这个问题吗?我也遇到了同样的问题。 - Scoota P
@ScootaP 嗯。我不记得我是否遇到过这个问题,但我有一个可用的身份验证解决方案。我编写了一个小型中间件来处理身份验证和用户管理。如果您想使用其中任何内容,我已将涉及的代码(希望我都有)添加到gist中。该代码尚未更新为express 4... - jonasonline
1个回答

0

我刚刚遇到了同样的问题并成功解决了。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);
    });

`


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接