node.js - PassportJS - FacebookTokenStrategy 返回 404

标签 node.js express passport.js passport-facebook

我正在使用 PassportJS 处理浏览器和移动客户端的 FB 身份验证。对于网络用户,我使用的是 Passport FacebookStrategy,它按预期工作。我还想允许移动客户端访问我的 API。我正在尝试使用 Passport FacebookTokenStrategy 来促进这一点。这似乎与一个小问题有关。当移动客户端向服务器发出 GET 请求时,将使用 FacebookTokenStrategy 并调用验证回调函数。在验证功能中,我可以看到用户配置文件可用,因此身份验证已成功。但是,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”回调?这在网络客户端的上下文中是有意义的,但我不确定应该如何使用 facebook-token 策略来处理。

最佳答案

我刚刚遇到了同样的问题并且能够解决它。由于中间件在 express 中的工作方式,返回 404。您需要传入第三个响应成功的函数。

第三个函数并不总是被调用。只有在前面的中间件成功时才会调用它。

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);
    });

`

关于node.js - PassportJS - FacebookTokenStrategy 返回 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20234852/

相关文章:

reactjs - 捆绑 React/Express 应用程序用于生产

node.js - Sequelize hooks,有什么方法可以获取 Express 的用户?

node.js - 使用 PassportJS for Facebook 时如何区分登录和注册

vue.js - 如何处理来自 Nuxt SSR 的 Passport js 重定向?

双重回调后的Javascript处理函数

javascript - 如何在 multer 中上传带有可选数据的图像?

node.js - Node js 从 utf-8 转换

node.js - Passport 使用 NextJs 和 Express 对每个请求调用 deserializeUser

javascript - Socket.IO双火

java - 在 Mac 中找不到 'JAVA_HOME' 环境变量