title: passport Error: Failed to serialize user into session
body: <p>I have a little problem with passport-local using a mySQL-DB.</p>
  <p>I get following error message when i try to login:</p>
  <blockquote>
  <p>Error: Failed to serialize user into session</p>
  <p>at /your/path/to/project/auth.js:11:5</p>
  </blockquote>
  <p>The query seems to be ok. I get the correct object as response.</p>
  <pre><code>[ RowDataPacket {
      id: 1,
      firstname: 'firstname',
      lastname: 'lastname',
      username: 'username',
      password: 'password in hash',
      role: 'role',
      enabled: 1 } ]
  </code></pre>
  <p>But if i</p>
  <pre><code>console.log(user) 
  </code></pre>
  <p>in</p>
  <pre><code>passport.serializeUser 
  </code></pre>
  <p>i get a</p>
  <pre><code>[] 
  </code></pre>
  <p>Thanks in advance :-)</p>
  <p><strong>The auth.js</strong></p>
  <pre><code>const passport = require('passport');
  const expressSession = require('express-session');
  const LocalStrategy = require('passport-local');
  const crypto = require('crypto');
  
  const userModel = require('./user/model');
  
  module.exports = app =&gt; {
    passport.serializeUser(function(user, done){
      //console.log(user.id);
      done(null, user.id);
    })
    //passport.serializeUser((user, done) =&gt; done(null, user.id));
    passport.deserializeUser((id, done) =&gt; {
      userModel.get({ id }).then(
        user =&gt; {
          if (!user) {
            done('user not found');
          }
          done(null, user);
        },
        err =&gt; done(err),
      );
    });
  
    passport.use(
      new LocalStrategy((username, password, done) =&gt; {
        
        const hash = crypto
          .createHash('md5')
          .update(password)
          .digest('hex');
          console.log({password: hash});
        userModel.get({ username, password: hash }).then(
          user =&gt; {
            if (!user) {
              done('user not found');
            }
            //console.log(user);
            done(null, user);
          },
          error =&gt; done('FEHLER: ' + error),
        );
      }),
    );
  
    app.use(
      expressSession({
        secret: 'geheim',
        resave: false,
        saveUninitialized: true,
        cookie: {secure: true}
      }),
    );
  
    app.use(passport.initialize());
    app.use(passport.session());
  
    app.post(
      '/login',
      passport.authenticate('local', { failureRedirect: '/login' }),
      (request, response) =&gt; {
        response.redirect('/');
      },
    );
    app.get('/logout', (req, res) =&gt; {
      req.logout();
      res.redirect('/');
    });
  };
  </code></pre>
  <p><strong>The model.js</strong></p>
  <pre><code>'use strict';
  const dbServerConnection = require('../controller/mysqlconnector.js');
  
  function get(query = {}) {
      return new Promise((resolve, reject) =&gt; {
        let queryElements = [];
        if (query) {
          for (let key in query) {
            queryElements.push(`${key} = ?`);
          }
        }
    
        const queryString =
          'SELECT * FROM usertable WHERE ' + queryElements.join(' AND ');
        dbServerConnection.query(queryString, Object.values(query), (error, results) =&gt; {
          if (error) {
            reject(error);
          } else {
              console.log(results);
            resolve(results);
          }
        });
      });
    }
    
    module.exports = {
      get,
    };
  </code></pre>