title: Getting error of "Can't serialize user", but the passport.serializeUser is never called
body: <p>My struggle with adding passport to my application continues! I got it so that I no longer get the crazy huge errors that I saw before, but the app just kept outputting <code>{}</code>. So after putting in as many <code>console.log()</code> statements as I could find, I've narrowed it down to the fact that the application says it can't serialize the user object, but as far as I can tell the <code>passport.serializeUser()</code> function is never called. </p>
  
  <p><strong><em><a href="https://github.com/jhechtf/hms-students/" rel="nofollow">GitHub link with all files (other than node modules)</a></em></strong> </p>
  
  <p>Granted, I am not 100% on how passport works, and I'm super new to Node but have experience with other languages for years, so if I need to I can just do this by hand, but I'd much prefer using passport as it allows for me to eventually use Facebook and Google auth without having to remake the wheel, so to speak. </p>
  
  <h2>Error</h2>
  
  <p>Here is the exact error:</p>
  
  <blockquote>
    <p>Passport Strategy  jhechtf (password)</p>
    
    <p>user found</p>
    
    <p>passwords are good</p>
    
    <p>Passport Authenticate :  jhechtf</p>
    
    <p>Passport Authenticate Err:  null</p>
    
    <p>Passport Authenticate Info:  { message: 'Logged In Successfully' }</p>
    
    <p>AuthController Error:  [Error: Failed to serialize user into session]</p>
  </blockquote>
  
  <p>Now I've googled "Failed to serialize user into session passport.js" in almost every term imaginable but every time it always seems to be something with the <code>deserializeUser</code> instead of <code>serializeUser</code>. The one thing that struck me was that my <code>console.log()</code> calls from the <code>serializeUser()</code> aren't seen at any point in time. </p>
  
  <h1>AuthController.js</h1>
  
  <pre><code>var passport = require('passport');
  
  module.exports = {
  
      _config: {
          actions: false,
          shortcuts: false,
          rest: false
      },
  
      login: function (req, res) {
  
          passport.authenticate('local', function (err, user, info) {
              if(user==false){
                  return res.status(403).send(info);
              }
  
              console.log("Passport Authenticate : ", user.username);
              console.log("Passport Authenticate Err: ", err);
              console.log("Passport Authenticate Info: ", info);
              req.logIn(user, function (err) {
                  if (err) {  
                      console.log("AuthController Error: " , err);
                      return res.status(401).send(err);
                  }
                  //Assume a correct login?
  
                  var red = req.session.redirectTo || '/'; 
                  //get the redirection value if it was set.
                  delete req.session.redirectTo;
                  //Now that we've got it, delete it.
                  res.redirect(red);
                  //redirect
              });
  
  
          })(req, res);
      },
  
      logout: function (req, res) {
          req.logout();
          res.redirect('/');
      }
  };
  </code></pre>
  
  <h1>config/passport.js</h1>
  
  <pre><code>var passport = require('passport'),
      LocalStrategy = require('passport-local').Strategy,
      bcrypt = require('bcrypt-nodejs');
  
  passport.serializeUser(function (user, done) {
      console.log("Serialize User (passport.js) ", user);
  //  if(user==false || user == undefined){
  //      done({error:'user is not a user object, but is false or undefined'});
  //  }
  //  done(null, user.user_id);
      done(null,user.user_id);
  });
  
  passport.deserializeUser(function (id, done) {
      console.log("ID COMING IN AS: ", id);
      Users.findOne({
          user_id: id
      }, function (err, user) {
          return done(err, user);
      });
  });
  
  passport.use(new LocalStrategy({
          usernameField: 'username',
          passwordField: 'user_password'
      },
      function (un, password, done) {
          console.log("Passport Strategy ", un,password);
  
          Users.findOne({
              username: un
          }, function (err, user) {
              if (err) {
                  console.log("USER.FINDONE ERROR ", err);
                  return done(err,false,{message:'Can\'t find user'});
              }
              if (!user) {
                  return done(null, false, {
                      message: 'Incorrect Username &lt;small&gt;Are you sure you signed up?&lt;/small&gt;'
                  });
              }
              console.log('user found');
              if (bcrypt.compareSync(password, user.user_password)) {
                  console.log('passwords are good');
                  return done(null, user, {
                      message: 'Logged In Successfully'
                  });
              } else {
                  return done(null, false, {
                      message: 'Invalid Password'
                  });
              }
  
          });
      }
  ));
  </code></pre>
  
  <h1>config/http.js (just in case)</h1>
  
  <pre><code>passportInit: require('passport').initialize(),
  passportSession: require('passport').session(),
  
  order: [
      'startRequestTimer',
      'cookieParser',
      'session',
      'myRequestLogger',
      'passportInit',
      'passportSession',
      'bodyParser',
      'handleBodyParserError',
      'compress',
      'methodOverride',
      'poweredBy',
      'router',
      'www',
      'favicon',
      '404',
      '500'
    ],
  </code></pre>
  
  <h1>SQL for the table</h1>
  
  <pre><code>CREATE TABLE `users` (
      `user_id` int(11) NOT NULL AUTO_INCREMENT,
      `username` varchar(32) NOT NULL,
      `user_password` varchar(255) DEFAULT NULL,
      `user_salt` varchar(100) NOT NULL,
      `user_email` varchar(255) NOT NULL,
      `user_first_name` varchar(100) DEFAULT NULL,
      `user_last_name` varchar(255) DEFAULT NULL,
      `user_display_name` varchar(255) DEFAULT NULL,
      UNIQUE KEY `user_id` (`user_id`)
      ) ENGINE=InnoDB
  </code></pre>
  
  <p>hanks for your time and patience. </p>