快速会话未保存为Ipad [英] express session not saving for Ipad

查看:63
本文介绍了快速会话未保存为Ipad的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为用户登录时保存一个会话变量.这可以在计算机上使用,但是当我使用 Safari Chrome iPad 上尝试时,它不会保存.

I am trying to save a session variable for a user when they login. This works on the computer, but when I try it on an iPad using Safari or Chrome it doesn't save.

在这里设置会话:

app.set('trust proxy', 1)
app.use(session({
  secret: crypto.randomBytes(20).toString('hex'),
  resave: false,
  duration: 60 * 60 * 1000,
  activeDuration: 10 * 60 * 1000,
  saveUninitialized: false,
  cookieName: 'session',
  cookie: { secure: true }
}))

我使用此路由来设置用户:

I use this route to set up the user:

.get('/checkLogin', (req,res) => {
  const loginCred = req.query;
  db.any('SELECT * FROM users WHERE user_name = $1 AND password = $2 LIMIT 1', [loginCred[0], loginCred[1]])
  .then(function (user) {
    req.session.user = user;
    req.session.save();
    res.end(JSON.stringify(user));
  })
  .catch(function (err) {
      throw err;
  })
})

当我在控制台上登录时,它已正确设置. 然后,当我在回程上调用会话时,它不存在.我试图添加保存,但仍然无法正常工作.我还在会话变量中添加了maxage,以使其保持活动状态3天,但仍然无法正常工作.

When I console log this, it is getting set properly. Then when I call the session on the return it is not there. I've tried to add the save and that still didn't work. I also added maxage to the session variable to keep it alive for 3 days and it still didn't work.

推荐答案

您的cookie为 {安全:true} ,它需要HTTPS连接,浏览器才能将会话cookie与请求一起发送回.但是建议您确保它适合您的测试环境,确保您在两个设备上都使用HTTPS

You have your cookie {secure: true} which requires a HTTPS connection for the browser to send the session cookie back with the request. However recommended make sure it fits your testing environment, make sure you're using HTTPS on both devices

https://www.npmjs.com/package/express-session#cookiesecure

还具有开发或生产模式,并在进行更改后重新启动express.每次重新启动后,您的秘密更改(使用函数[crypto.randomBytes(20).toString('hex')]而不是静态密钥) 导致重新启动后客户端的会话ID无效;无论如何,这都不重要,因为您没有设置持久性会话,因此任何重新启动都会擦除所有会话. 如果您需要持久会话,请使用memcached,数据库或文件而不是进程内存进行签入

Also with development or production mode and restarting express after making changes your secret changes after every restart (from using a function [crypto.randomBytes(20).toString('hex')] instead of a static key) causing a client's session ID to be invalid after restart; which shouldn't matter anyway cause you have no persistent sessions setup so any restart will wipe all sessions. If you need persistent sessions check into using memcached, database, or file instead of process memory

https://www.npmjs.com/package/express-session#store

这篇关于快速会话未保存为Ipad的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆