gpt4 book ai didi

javascript - 无法使用 req.session 在特定路由中设置未定义的属性 'X'

转载 作者:行者123 更新时间:2023-12-03 02:28:52 27 4
gpt4 key购买 nike

目前正在合作:

  • NodeJS
  • express
  • MySQL
  • 快速 session
  • Express-mysql-session

最近我正在制作一个带有重定向的简单登录表单,我希望在用户成功连接时创建一个 session 。

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

req.session.authenticated = true;

但这给了我错误:

Cannot set property 'authenticated' of undefined

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

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 中添加 session 和 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)
});
}

关于javascript - 无法使用 req.session 在特定路由中设置未定义的属性 'X',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48826071/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com