我在看一本nodejs的书:https://www.packtpub.com/web-development/mean-web-development
在我尝试使用Passport模块登录之前,一切都很顺利。它只是挂着没有错误。我尝试过其他解决方案,比如重新订购中间件,但我认为它们可能适用于不同版本的express,因为有些语法看起来略有不同(而且不起作用)。以下是相关部分。任何帮助都将不胜感激!
package.json包:

{
"name": "MEAN",
"version": "0.0.3",
"description": "First mean app",
"dependencies": {
  "express":            "~4.8.8",
  "morgan":             "~1.3.0",
  "compression":        "~1.0.11",
  "body-parser":        "~1.8.0",
  "method-override":    "~2.2.0",
  "express-session":    "~1.7.6",
  "ejs":                "~1.0.0",
  "connect-flash":      "~0.1.1",
  "mongoose":           "~3.8.15",
  "passport":           "~0.2.1",
  "passport-local":     "~1.0.0"
}
}

快递.js:
module.exports = function(){
var app = express();



if(process.env.NODE_ENV === 'development'){
    app.use(morgan('dev'));
}else if(process.env.NODE_ENV === 'production'){
    app.use(compress());
}

app.use(bodyParser.urlencoded({
    extended: true
}));
app.use(bodyParser.json());
app.use(methodOverride());

app.use(session({
   saveUnitialized: true,
   resave: true,
   secret: config.sessionSecret
}));


app.set('views', './app/views');
app.set('view engine', 'ejs');

app.use(flash());
app.use(passport.initialize());
app.use(passport.session());


require('../app/routes/index.server.routes.js')(app);
require('../app/routes/users.server.routes.js')(app);

app.use(express.static('./public'));

return app;
};

护照.js
module.exports = function(){

var User = mongoose.model('User');

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

passport.deserializeUser(function(id, done){
   User.findOne({
       _id: id
   }, '-password -salt'), function(err, user){
       done(err, user);
   };
});

require('./strategies/local.js')();
};

路由:
 app.route('/signin')
    .get(users.renderSignin)
    .post(passport.authenticate('local',{
        successRedirect: '/',
        failureRedirect: '/signin',
        failureFlash: true
    }));

编辑:似乎是passport.deserializeuser函数出现问题

最佳答案

好吧,我去看了护照文件,然后贴上了密码,看起来很管用。所以…你知道为什么这本书的例子不起作用吗?
有效的代码:

passport.deserializeUser(function(id, done) {
   User.findById(id, function(err, user) {
     done(err, user);
   });
 });

07-24 19:39