8

I'm trying to do my first steps with the Express framework for Node. I was trying to implement a tiny authentication example, using Passport. However, I can't make it work; I keep getting the error: Error: failed to serialize user into session.

I installed node-inspector to try to see what's going on. Apparently, my serialization function is being called, and it executes done(null, 0) as expected. I tried taking a look at Passport code, but I couldn't understand what the problem is. This is pretty much my first attempt at a Node application so I'm not familiar with the code. Can someone give me a hint? Thanks.

var express = require('express');
var jade = require('jade');

var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;

var app = express();

/*
 * Settings
 */

app.configure(function(){
    app.set('views', __dirname + '/views');
    app.set('view engine', 'jade');

    app.use(express.logger());
    app.use(express.cookieParser());
    app.use(express.bodyParser());
    app.use(express.session({ secret: 'cat in the bag' }));
    app.use(passport.initialize());
    app.use(passport.session());

    passport.use(new LocalStrategy(
        function(username, password, done) {
            done(null, { id: 0, username: 'juancito' });
        }
    ));

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

    passport.deserializeUser(function(id, done) {
        done(null, 'juancito');
    });
});



/*
 * Routes
 */

app.get('/', function(req, res) {
    res.render('index', { title: 'Welcome!' });
});

app.get('/login', function(req, res) {
    if (req.user)
        return res.redirect('/');

    res.render('login', { title: 'Log in' });
});

app.post('/login',
    passport.authenticate('local', {
        successRedirect: '/',
        failureRedirect: '/login'
    })
);

app.get('/logout', function(req, res) {
    req.logOut();
    res.redirect('/');
});

app.listen(3000);
console.log('Listening on port 3000.');
1
  • Downvoted because this is a common symptom, but this is a strange cause (id == 0). My ids (like most people's ids) are all non-0, and I am still getting this error. Sep 22, 2014 at 17:04

2 Answers 2

11

The issue is that 0 is a false-y value in JavaScript, so Passport thinks you haven't serialized your user.

done(null, 0)  // don't serialize users to a 0 number

I'd recommend starting user IDs at 1 (which happens in SQL databases), or (if you really need integers beginning at 0) serialize them as strings to the session.

done(null, 0.toString())
2
  • Thanks, Jared! You were right. So, 1, 2, 3 are valid serializations for objects, but 0 isn't because the language considers 0 a falsy value. Isn't that a bug in the lib? Oct 24, 2012 at 5:01
  • 1
    Yeah, feel free to file an issue on GitHub. Oct 24, 2012 at 21:37
5

Maybe you should assign your serialize/deserialize callbacks before calling passport.initialize() and passport.session().

passport.use(new LocalStrategy(
    function(username, password, done) {
        done(null, { id: 0, username: 'juancito' });
    }
));

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

passport.deserializeUser(function(id, done) {
    done(null, 'juancito');
});

app.configure(function(){
    app.set('views', __dirname + '/views');
    app.set('view engine', 'jade');

    app.use(express.logger());
    app.use(express.cookieParser());
    app.use(express.bodyParser());
    app.use(express.session({ secret: 'cat in the bag' }));
    app.use(passport.initialize());
    app.use(passport.session());

});
3
  • Spot on. I had the error appearing only after I tried to access a page with a session restored from the database. Since I had the serialize/ deserialize set only after the first login the server has processed.
    – Naor Biton
    Mar 10, 2014 at 7:06
  • This made no difference for me. Sep 22, 2014 at 17:03
  • Just saw this, if it still doesn't work try doing done(null, toString(user.id)) instead of done(null, user.id)
    – gfdb
    Feb 23, 2022 at 11:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.