title: passport with express: serialization issue with custom authentication method
body: <pre><code>   #set up passport
  LocalStrategy = require('passport-local').Strategy
  passport.use(new LocalStrategy({usernameField: 'email'}, (email, password, done) -&gt;
  
      User.findOne({ email: email }, (err, user) -&gt;
        console.log("findone")
        if err?
           console.log "error"
           return done(err)
        if not user
          console.log("incorrect username")
          return done(null, false, { message: 'Incorrect username.' })
        if password isnt user.password
            console.log "NOT"
            return done(null, false, {message: "Incorrect password" })
        console.log "BUT YESS"
        done(null,user)
      )
  ))
  
  
   passport.serializeUser((user, done) -&gt;
            done(null, user._id)
      )
  
      passport.deserializeUser((id, done) -&gt;
            User.findById(id, (err, user) -&gt;
               done(err, user)
            )
      )
  </code></pre>
  
  <p>I am using the LocalStrategy example as found at <a href="http://passportjs.org/guide/username-password/" rel="nofollow">http://passportjs.org/guide/username-password/</a></p>
  
  <p>Now, my problem is that if I enter a wrong password, or an incorrect username, I get an error <code>Error: failed to serialize user into session</code>.</p>
  
  <p>In fact, in my serializeUser function, <code>user</code> is <code>false</code>. This probably is because the code literally returns false as the second parameter above:
  <code>return done(null, false, {message: "Incorrect password"</code> })</p>
  
  <p>But this is from the docs! So what am I doing wrong? Why do I have a serialization problem when parameters are incorrect? When parameters are ok (username and password correct), I can login without problems</p>
  
  <p>EDIT: Upon comment by @robertklep here is how I use the authentication function. Indeed, because I need to route differenly depending on role, I use the passport custom method (this detail added to title of question):</p>
  
  <pre><code>app.post '/login', (req, res, next) -&gt;
    passport.authenticate('local', (err, user, info) -&gt;
      return next(err) if err?
      return res.redirect('/login', { message: req.flash('Access denied')}) if not user?
      req.logIn user, (err) -&gt;
        if err?
          console.log "err! " + err
          res.redirect("/", { message: req.flash(err)})
          return
        if user.role is "admin" or user.role is "auditor"        
          res.redirect("/dashboard")
        else
          res.redirect("/start") 
    )(req, res, next)
  </code></pre>