使用带护照本地的passport-facebook策略时,无法将用户序列化为会话

时间:2015-04-12 06:59:02

标签: node.js sails.js passport.js passport-local passport-facebook

我在passport-local中使用passport-facebooksails.js策略进行身份验证。使用电子邮件验证工作正常。但是当用户使用Facebook进行身份验证时,我收到此错误消息[Error: Failed to serialize user into session]

然后我测试了serializeUser方法,结果发现user param在facebook的情况下是空的。虽然我也试图查看是否调用verifyHandler并且没有调用它。

以下是我的facebook身份验证操作代码:

facebook: function (req, res) {

    passport.authenticate('facebook', {failureRedirect: '/login', scope: ['email']}, function (err, user) {

      if ((err) || (!user)) {
        req.session.flash = {
          errMsg: 'Email or password mismatch.'
        }

        return res.redirect('/login');
      }

      req.logIn(user, function (err) {
        if (err) {
          console.log(err);
          res.view('500');
          return;
        }

        res.redirect('/');
        return;
      });
    })(req, res);

  }

这是passport.js服务的代码(api / services / passport.js)

var passport = require('passport'),
  LocalStrategy = require('passport-local').Strategy,
  FacebookStrategy = require('passport-facebook').Strategy,
  bcrypt = require('bcrypt');


var verifyHandler = function (token, tokenSecret, profile, done) {

  console.log('in verifyHandler'); // this line is not being executed.
  console.log(profile);

  process.nextTick(function () {

    User.findOne({uid: profile.id}, function (err, user) {

      if (err) {
        return done(err);
      }

      if (user) {

        return done(null, user);

      } else {

        var data = {
          provider: profile.provider,
          uid: profile.id,
          name: profile.displayName
        };

        if (profile.emails && profile.emails[0] && profile.emails[0].value) {
          data.email = profile.emails[0].value;
        }

        User.create(data, function (err, user) {
          return done(err, user);
        });
      }
    });
  });
};

passport.serializeUser(function (user, done) {
  done(null, user.id);
});

passport.deserializeUser(function (id, done) {
  User.findById(id, function (err, user) {
    done(err, user);
  });
});

passport.use(new LocalStrategy({
    usernameField: 'email',
    passwordField: 'password'
  },
  function (email, password, done) {

    User.findOne({email: email}).exec(function (err, user) {
      if (err) {
        return done(err);
      }
      if (!user) {
        return done(null, false, {message: 'Unknown user ' + email});
      }

      bcrypt.compare(password, user.password, function (err, res) {
        if (!res) return done(null, false, {message: 'Invalid Password'});
        return done(null, user);
      });

    });
  }
));

passport.use(new FacebookStrategy({
  clientID: sails.config.facebook.clientID,
  clientSecret: sails.config.facebook.clientSecret,
  callbackURL: sails.config.facebook.callbackURL
}, verifyHandler));

最后(config / passport.js)

var passport = require('passport'),
  LocalStrategy = require('passport-local').Strategy,
  FacebookStrategy = require('passport-facebook').Strategy;

module.exports = {
  http: {
    customMiddleware: function (app) {
      app.use(passport.initialize());
      app.use(passport.session());
    }
  }
};

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

检查user.id是否已定义且是字符串但不是ObjectId()。

in

passport.serializeUser(function (user, done) {
  done(null, user.id);
});