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

时间:2014-01-13 20:02:49

标签: passport.js

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

奇怪的是,serializeUser(function..被称为正常......

但更奇怪的是,它在几天前工作得很好,but now it isn't.我无法想到我在系统中发生的任何可能导致这种情况的事情。

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

2 个答案:

答案 0 :(得分:8)

在 之后移动app.use(passport.… app.use(express.…解决了它。

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

答案 1 :(得分:4)

从快递v4.x开始,同样的答案仍然适用于护照。(...)必须在express.session之后调用:

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

您不再在app.configure()内调用它们,因为它已从快递v4.x中弃用