Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What could be causing deserializeUser() to not get called?

Tags:

passport.js

I'm using passport with nodejs and I'm having a strange problem, the passport.deserializeUser(function.. never gets called.

Strange thing is that serializeUser(function.. get's called just fine..

And yet stranger thing is that it was working just fine a couple days ago, but now it isn't. I can't think of anything that I changed in my system that would cause this.

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

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

app.configure(function(){
    app.use(passport.initialize());
    app.use(passport.session());
    app.use(express.static('public'));
    app.use(express.cookieParser());
    app.use(express.bodyParser());
    app.use(express.session({ secret: 'keyboard cat' }));
    app.use(app.router);
});

passport.use(new LocalStrategy(function(username, password, done){
    return done(null, 'Always Authenticated User');
}));

passport.serializeUser(function(user, done) {
    console.log(' serialize OK! ');
    done(null, user);
});
passport.deserializeUser(function(id, done) {
    console.log('deserialize Never gets called');
    done(null,id);
});

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

app.get('/', function(req, res){
    // very simple form
    res.send("<form id='LoginLocal' action='/login' method='post'><fieldset><legend>Login with username/password</legend><label for='username'> Username: <input type='text' name='username' placeholder='username'><label for='password'> Password: <input type='password' name='password' placeholder='password'><input type='submit' value='Login'></fieldset></form>");
});

app.listen(80);
like image 270
laggingreflex Avatar asked Jan 13 '14 20:01

laggingreflex


Video Answer


2 Answers

Moving the app.use(passport.… after the app.use(express.… solved it.

app.configure(function(){
    app.use(express.static('public'));
    app.use(express.cookieParser());
    app.use(express.bodyParser());
    app.use(express.session({ secret: 'keyboard cat' }));
    app.use(passport.initialize());
    app.use(passport.session());
    app.use(app.router);
});
like image 100
laggingreflex Avatar answered Sep 21 '22 16:09

laggingreflex


As of express v4.x the same answer still applies that passport.(...) must be called only after express.session like so:

app.use(express.session({ secret: 'keyboard cat' }));
app.use(passport.initialize());
app.use(passport.session());

You no longer call them inside app.configure() as it has been deprecated as of express v4.x

like image 42
pangolin Avatar answered Sep 24 '22 16:09

pangolin