passportjs and passportlocal add users "Error: failed to serialize user into session"

I have followed the code here - > https://github.com/jaredhanson/passport-local/tree/master/examples/express3 for add local authentication for users.

The problem is when I try to add users,

So I created this route

app.get('/signup', function(req,res){
  res.render('/signup');
});

app.post('/signup', function(req,res){
  var body = req.body;
  users.push(body);
  res.redirect('/');
});

Then the page w the form It's

form(method='POST', action='/signup')
 input(type='text', name='username', placeholder='username')
 input(type='text', name='password', placeholder='password')
 button.btn Register

The dummy DB It's the one on the example

users = [
  {id:1, username: 'test', password:'papapa'}
];

So when I send the info w the form, all goes ok, but when I try to log in with the new created user, tells me "Error: failed to serialize user into session"

the serializeUser is this

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

passport.deserializeUser(function(id, done) {
  findById(id, function (err, user) {
    done(err, user);
  });
});

The user being pushed in the POST /signup route will not have an ID, which the examples serialization code expects.

Change it to something like this, and it should work.

app.post('/signup', function(req,res){
  var body = req.body;
  body.id = users.length;
  users.push(body);
  res.redirect('/');
});

(Note that this is an example only, and not recommended for "real" apps.)

I guess you are missing the session serializer. Take a look to https://github.com/jaredhanson/passport, section 'Sessions'.

Basically, you need two functions, to save the current user into the session (passport.serializeUser) and to read it back (passport.deserializeUser).