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);
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);
});
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With