user2244925 user2244925 - 1 year ago 658
Node.js Question

Error: failed to serialize user into session

hi got a problem with the passport module and express.

This is my code and I just want to use a hardcoded login for the first try.

I always get the message:

I searched a lot and found some posts in stackoverflow but I didnt get the failure.

Error: failed to serialize user into session
at pass (c:\Development\private\aortmann\bootstrap_blog\node_modules\passport\lib\passport\index.js:275:19)

My code looks like this.

'use strict';

var express = require('express');
var path = require('path');
var fs = require('fs');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var nodemailer = require('nodemailer');

var app = express();

module.exports = function setupBlog(mailTransport, database){
var config = JSON.parse(fs.readFileSync('./blog.config'));

app.set('view options', {layout: false});

app.use(express.static(path.join(__dirname, '../', 'resources', 'html')));

app.use(express.session({ secret: 'secret' }));

app.get('/blog/:blogTitle', function(req, res) {
var blogTitle = req.params.blogTitle;
if(blogTitle === 'newest'){
database.getLatestBlogPost(function(post) {
} else {
database.getBlogPostByTitle(blogTitle, function(blogPost) {

passport.use(new LocalStrategy(function(username, password, done) {
// database.login(username, password, done);
if (username === 'admin' && password === 'admin')
done(null, { username: username });
else {
done(null, false);
}));'/login', passport.authenticate('local', {
successRedirect: '/accessed',
failureRedirect: '/access'

console.log('Blog is running on port 8080');


Answer Source

It looks like you didn't implement passport.serializeUser and passport.deserializeUser. Try adding this:

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

passport.deserializeUser(function(user, done) {
  done(null, user);