我为文凭项目创建了一个在线商店,我使用用户名和密码登录,在Cookie中写入用户的姓名和姓氏,然后将其读取,然后变得不可用。我以为问题是cookie的生存期,但是在设置maxAge值:1000 * 60 * 60和httpOnly:false之后,一切都没有改变。通过get方法编写cookie时,所有cookie均保留,而通过post方法编写cookie时,则不保存它们 这是用于创建和发送cookie的代码:
app.post("/signIn", urlencodedParser, function (request, response) {
if(!request.body) return response.sendStatus(400);
connection.query(`SELECT user_name,
user_surname
FROM users where (${(request.body.login.match(/^\d+$/) !== null) ?
`telephone=${request.body.login}` :
`email='${request.body.login}'`}) and
password='${request.body.password}'`,
(err, users) => {
if(err == null && (typeof users[0] === "object" && users[0] !== undefined)) {
response.cookie('userName', users[0].user_name);
response.cookie('userSurname', users[0].user_surname);
response.cookie('successAuthentication', 'true');
response.sendFile(`${__dirname}/public/html/index.html`);
} else if(err == null && users[0] === undefined) {
connection.query(`SELECT worker_name,
worker_surname,
position_name
FROM worker inner join position on
worker_id = position_id where (${(request.body.login.match(/^\d+$/) !== null) ?
`telephone=${request.body.login}` :
`email='${request.body.login}'`}) and
password='${request.body.password}'`, (workerSelectErr, worker) => {
if(workerSelectErr == null && worker !== []) {
response.cookie('workerName', worker[0].worker_name);
response.cookie('workerSurname', worker[0].worker_surname);
response.cookie('successAuthentication', 'true');
response.sendFile(`${__dirname}/public/html/indexWorker.html`);
}
});
} else if(err != null) {
throw new Error(err);
}
});
});