什么可能导致 deserializeUser() 不被调用?

问题描述:

我正在使用带有 nodejs 的护照,但遇到了一个奇怪的问题,passport.deserializeUser(function.. 永远不会被调用.

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

奇怪的是 serializeUser(function.. get 被调用得很好..

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

但更奇怪的是,几天前它工作得很好,但现在它不是't.我想不出我在系统中所做的任何更改会导致这种情况.

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);

从 express v4.x 开始,相同的答案仍然适用于该护照.(...) 必须仅在 express.session 之后调用,如下所示:

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());

您不再在 app.configure() 中调用它们,因为它从 express v4.x 开始已被弃用

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