无法在具有 req.session 的特定路由中设置未定义的 属性 'X'

Cannot set property 'X' of undefined in specific route with req.session

目前正在与:

最近我正在做一个带有重定向的简单登录表单,我想在用户成功连接时创建一个会话。

当密码验证成功时,我尝试这样做:

req.session.authenticated = true;

但这给了我错误:

Cannot set property 'authenticated' of undefined

为了处理会话,我有以下代码:

server.js

var express    = require('express');   
var session = require('express-session');
var MySQLStore = require('express-mysql-session')(session);
var options = {
    host: 'localhost',
    port: 3306,
    user: 'myuser',
    password: 'mypassword',
    database: 'mydb_repo'
};
var router = express.Router();            
var sessionStore = new MySQLStore(options);
router.use(session({
    key: 'session_cookie_name',
    secret: 'session_cookie_secret',
    store: sessionStore,
    resave: false,
    saveUninitialized: false,
    path: "/",
    // How frequently expired sessions will be cleared; milliseconds:
    checkExpirationInterval: 900000,
    // The maximum age of a valid session; milliseconds:
    expiration: 86400000
}));
// middleware to use for all requests
router.use(function(req, res, next) {
    if (req.url === '/bakery' && (!req.session || !req.session.authenticated)) {
        res.send('unauthorised');
        return;
    }else{
      // do logging
      var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
      console.log('-- EVENT -- The wolf is in the pen. '+ip);
      next(); // make sure we go to the next routes and don't stop here
    }
});
require('./routes')(router);

/routes/index.js

var fs = require('fs');
module.exports = function(router){
    fs.readdirSync(__dirname).forEach(function(file) {
        if (file == "index.js") return;
        var name = file.substr(0, file.indexOf('.'));
        require('./' + name)(router);
    });
}

/routes/oven.js

module.exports = function(router){
    router.get('/oven/', function(req, res){
      var text = fs.readFileSync('./app/views/oven.html','utf8')
      var content = text.replace("[TITLE]", "Cookie Touch");
        res.send(content);
    });
    router.route('/oven')
      .post(function(req, res) {
        var email = req.body.inputEmail;
        var pass = req.body.inputPassword;
        console.warn("-- WARN -- A new cookie in the Oven: "+email);
        authentificate(email, pass, req, res);
      });
}
function authentificate(email, pass, req, res){
    //get salt and hash passes (long code)
    checkPass(email, hash_f, res);
  });
}
function checkPass(email, hash, req, res){
  connection.query('SELECT password AS pass FROM user WHERE email="'+email+'"', function (error, results, fields) {
    if (results[0].pass == hash){
      req.session.authenticated = true;
      res.redirect('/api/bakery');
      console.log("Successfully authentificated!");
    }else{
      res.send("Wrong Password");
      console.log("Wrong Password");
    }
  });
}

我尝试在 oven.js 中添加会话的定义和 router.use 但没有成功,而不是 router.use(session... 我也试过 app.use(session...

您正在使用只有三个参数的 checkPass checkPass(email, hash_f, res),而您应该传递四个参数。即req没有通过

function authentificate(email, pass, req, res){
    //get salt and hash passes (long code)
    checkPass(email, hash_f, req, res)
  });
}