销毁会话后,不会从浏览器中删除Express Session Cookieconnect.sid

问题描述

我有一个Vue.js SPA和一个由Express.js构建的Node.js API。我正在使用Express-Session(^ 1.11.3)来管理会话,并使用Express-Sequelize-Session(0.4.0)通过Sequelize将会话持久化在Postgres DB上,因为我需要一个会话才能使用Passport-azure -ad采用oidc策略。

一段时间后,我在使用Microsoft帐户登录时遇到了一些问题,并得出结论,这是因为从未从浏览器中清除会话cookie(connect.sid)。

我有些东西配置错误并进行了一些更改,但是即使进行了所有更改,它仍然无法正常工作。

通过以下方式配置Express应用上的会话:

import session from 'express-session';
import expressSequelizeSession from 'express-sequelize-session';

const Store = expressSequelizeSession(session.Store);

app.use(session({
  cookie: {
    path: '/',httpOnly: true,secure: env !== 'development',// On environments that have SSL enable this should be set to true.
    maxAge: null,sameSite: false,// Needs to be false otherwise Microsoft auth doesn't work.
  },secret: config.secrets.session,saveUninitialized: false,resave: false,unset: 'destroy',store: new Store(sqldb.sequelize),}));

在FE上,我将Vue.js与Axios一起使用,并将withCredentials设置为true,以便在HTTP请求上传递cookie。

// Base configuration.
import Axios from 'axios';

Axios.defaults.baseURL = config.apiURL;
Axios.defaults.headers.common.Accept = 'application/json';
Vue.$http = Axios;

// When making request.
Vue.$http[action](url,payload,{ withCredentials: true }).then(() => // Handle request);

从图像中可以看到,cookie是应注销请求发送的。

enter image description here

注销时,我碰到了这个端点并破坏了会话as is explained on the documentation

router.post('/logout',(req,res) => {
  try {
    req.session.destroy(() => {
      return responses.responseWithResultAsync(res); // Helper method that logs and returns status code 200.
    });

    return responses.handleErrorAsync(res); // Helper method that logs and returns status code 500.
  } catch (error) {
    return responses.handleErrorAsync(res,error); // Helper method that logs and returns status code 500.
  }
});

有趣的是,数据库上的会话已删除,因此我知道cookie是在具有正确会话ID的请求上正确发送的,但由于某种原因并未在浏览器中删除它。注销后,我仍然有这个:

enter image description here

有人知道我在做什么错吗?我感到奇怪的是,该会话已成功在数据库上删除,但未在请求上删除。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)