继续在序列化和反序列化PassportJS用户时遇到问题

时间:2014-08-26 13:10:44

标签: express passport.js sequelize.js

我正在将sequelize.js用于我的数据存储,将github策略用于passport.js。我的代码是:

app.use(passport.initialize());

app.use(passport.session());

passport.use(new GitHubStrategy({
  clientID: '123',
  clientSecret: '456',
  callbackURL: "" + config.hostname + ":" + config.port + "/auth/github/callback"
}, function(accessToken, refreshToken, profile, done) {
  var authProviderQuery, newUser;
  console.log('accessToken');
  console.log(accessToken);
  authProviderQuery = {
    authId: profile.id
  };
  newUser = {
    name: profile.username,
    email: profile.emails[0].value
  };
  return DB.AuthProvider.find(authProviderQuery).complete(function(err, dbAuthProvider) {
    var prop, _results;
    if (err) {
      return done(err);
    }
    if (!dbAuthProvider) {
      return DB.User.create(newUser).complete(function(err, dbUser) {
        var newAuthProvider;
        if (err) {
          return done(err);
        }
        newAuthProvider = {
          name: 'github',
          oathToken: accessToken,
          authId: profile.id,
          rawJSON: profile._raw
        };
        return dbUser.createAuthProvider(newAuthProvider).complete(function(err) {
          if (err) {
            return done(err);
          }
          return done(null, dbUser);
        });
      });
    } else {
      console.log(dbAuthProvider);
      _results = [];
      for (prop in dbAuthProvider) {
        _results.push(console.log(prop));
      }
      return _results;
    }
  });
}));

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

passport.deserializeUser = function(obj, done) {
  return DB.User.find({
    where: {
      id: obj.id
    }
  }).complete(function(err, dbUser) {
    console.log(dbUser);
    return done(err, dbUser);
  });
};

app.get('/auth/github', passport.authenticate('github'));

app.get("/auth/github/callback", passport.authenticate('github', {
  failureRedirect: "/login"
}), function(req, res) {
  return console.log('authenticated');
});

当我转到/auth/github时,我收到以下错误:

/Users/mysite/node_modules/mysql/lib/protocol/Parser.js:82
        throw err;
              ^
TypeError: object is not a function

我做错了什么?

1 个答案:

答案 0 :(得分:2)

您正在从Passport中替换serializeUserdeserializeUser方法,而不是调用它们来分配(de)序列化程序 - 即。你有

passport.deserializeUser = function(obj, done) {
  ...
};

何时应该

passport.deserializeUser(function(obj, done) {
  ...
});

请参阅文档:http://passportjs.org/guide/configure/