title: Server returning "Error: Failed to serialize user into session" when attempting to use register user using Passport.js and AJAX
body: <p>Server returning &quot;Error: Failed to serialize user into session&quot; when attempting to use register user using Passport.js and AJAX. The code works when not using AJAX from the registration page, but I want to have a popup that processes the registration too.</p>
  <p>passport.js</p>
  <pre><code>var passport = require('passport');
  var LocalStrategy = require('passport-local').Strategy;
  
  var User = require('../models/user');
  
  passport.serializeUser((user, done) =&gt; {
      done(null, user.id);
  });
  
  passport.deserializeUser((id, done) =&gt; {
      User.findById(id, (err, user) =&gt; {
          done(err, user);
      });
  });
  
  passport.use('local.popsignup', new LocalStrategy({
      usernameField: 'popUsername',
      passwordField: 'popPassword',
      passReqToCallback: true
  }, (req, email, password, done) =&gt; {
     User.findOne({'email':email}, (err, user) =&gt; {
  
         if(err){
             return done(err);
         }
  
         if(user){
             return done(null, false, req.flash('error', 'Email already exists, please login.'))
         }
  
         var newUser = new User();
         newUser.email = email;
         newUser.password = newUser.encryptPassword(password);
         newUser.preferences = preferences;
         newUser.save((err) =&gt; {
             return done(null, newUser);
         });
  
     }); 
  }));
  </code></pre>
  <p>user router</p>
  <pre><code>module.exports = (app, passport) =&gt; {
      app.post('/popover', function(req, res, next) {
          passport.authenticate('local.popsignup', function(err, user, info) {
              if (user !== undefined) {loggedIn = true} else {loggedIn = false};
              var errors = req.flash('error')
              req.login(user, function(err) {
                  if (err) return next(err);
                  res.status(200).send({errors: errors, loggedIn: loggedIn});
              });
          })(req, res, next);
      });
  }
  </code></pre>
  <p>view ajax js</p>
  <pre><code>            $.ajax({
                  url: &quot;/popover&quot;,
                  type: &quot;POST&quot;,
                  data: $('#popform-tag').serialize(),
                  success: function(data){
                      if(data.loggedIn == false) {
                          $(&quot;#email_alert&quot;).text(data.errors[0]).show();
                      } else {
                          $('#popover').modal('hide')
                          localStorage.setItem('preferences', 1);
                      }
                      //console.log(data);
                  },
                  error: function(XMLHttpRequest, textStatus, errorThrown) {
                      console.log(&quot;Status: &quot; + textStatus);
                      console.log(&quot;Error: &quot; + errorThrown);
                  }
              })
  </code></pre>