简体   繁体   English

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

[英]Facebook authentication using Passport in Node.js

I am implementing a Facebook authentication using Passport in node.js. 我正在node.js中使用Passport实现Facebook身份验证。 I can get the users profile from Facebook successfully. 我可以从Facebook成功获取用户个人资料。 The problem is, the code should redirect the user to the successRedirect: /profile but instead it always goes to /fbFailure. 问题是,代码应将用户重定向到successRedirect:/ profile,但始终将其转到/ fbFailure。 No matter what I tried, it always goes to the url : somesite.com:6161/fbFailure# = 无论我尝试了什么,它总是转到URL:somesite.com:6161/fbFailure# =

Passport middleware code: 护照中间件代码:

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

My Routes: 我的路线:

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

Console Output: 控制台输出:

json: 
   { id: '10154130288931542',
     name: ‘Tom Verra’,
     picture: { data: [Object] },
     email: ‘tom.vera@mysite.com' } }
GET /facebookSuccess?code=AQBzdIfKPQ..............YU6TvYuf......

Any help would be highly appreciated. 任何帮助将不胜感激。 Thanks in advance. 提前致谢。

You could try redirecting using res.redirect('/profile'); 您可以尝试使用res.redirect('/profile');重定向res.redirect('/profile'); , just the way it is done in the passport-facebook readme on GitHub ( https://github.com/jaredhanson/passport-facebook#authenticate-requests ): ,方法与在GitHub( https://github.com/jaredhanson/passport-facebook#authenticate-requests )上的Passport-facebook自述文件中完成的方式相同:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM