使用Node.js中的Passport进行Facebook身份验证

时间:2016-03-31 11:28:31

标签: node.js facebook authentication passport.js

我正在使用node.js中的Passport实现Facebook身份验证。我可以成功从Facebook获取用户个人资料。问题是,代码应该将用户重定向到successRedirect:/ profile,但它总是转到/ fbFailure。无论我尝试什么,它总是去网址:somesite.com:6161/fbFailure# =

Passport中间件代码:

{
   "book": [

      {
         "id":"01",
         "language": "Java",
         "edition": "third",
         "author": "Herbert Schildt"
      },

      {
         "id":"07",
         "language": "C++",
         "edition": "second",
         "author": "E.Balagurusamy"
      },
      {
         "id":"08",
         "language": "Java",
         "edition": "second",
         "author": "Gihan"
      }
   ]
}

我的路线:

var FacebookStrategy = require('passport-facebook').Strategy;
var models = require('../models');
passport.use('facebook', new FacebookStrategy(
{
    clientID: '1735552370024305', clientSecret: 'a004f1aee925b615022d7573984d5c26', callbackURL: "http://www.somesite.com:6161/facebookSuccess", profileFields: ['id', 'displayName', 'photos', 'emails'],
},
function(access_token, refreshToken, profile, done) {
    console.log('profile', profile);
models.Member.findOne({ '__id' : profile.id }, function(err, user) {
  if (err)
    return done(err); 
  if(user){
    return done(null, user);
  }
  done(null, user);
})
}
));

控制台输出:

app.get('/facebook', passport.authenticate('facebook',  { scope : 'email' }));
app.get('/facebookSuccess', passport.authenticate('facebook', {
        successRedirect : '/profile',
        failureRedirect : '/fbFailure'
    }));

app.get('/profile', function(req, res, next) {
 res.send('Successfully authenticated');
});

app.get('/fbFailure', function(req, res, next) {
 res.send('Failed to authenticate');
});

任何帮助都将受到高度赞赏。提前谢谢。

1 个答案:

答案 0 :(得分:0)

您可以尝试使用res.redirect('/profile');重定向,就像在GitHub上的passport-facebook自述文件中所做的那样(https://github.com/jaredhanson/passport-facebook#authenticate-requests):

app.get('/facebookSuccess',
  passport.authenticate('facebook', { failureRedirect: '/fbFailure' }),
  function(req, res) {
    res.redirect('/profile');
  });