当用户退出会话时如何正确删除cookie?

Ras*_*rov 5 authentication cookies amazon-cognito

背景:

对于身份验证,我们使用 AWS Cognito。我们的项目包含一个 API 服务器和一个 Web 服务器。在客户端,当用户登录应用程序时,我们将用户名和密码发送到 cognito 实例,该实例返回 JWT 访问令牌。我们在 cookie 中设置访问令牌并将用户重定向到主页。在 http 服务器端,对于私有页面的每个请求,我们都会检查 cookie 并验证 JWT 访问令牌。同样,在 API 端,对于每个请求,我们都会检查 cookie 并验证访问令牌。

当用户从应用程序注销时,我们会删除 cookie 并从 cognito 中注销。这是删除 cookie 的代码 -

function clearCookie(name: string, domain: string, path: string) {
  const derivedDomain = domain || document.domain;
  const derivedPath = path || "/";
  document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:01 GMT; domain=${derivedDomain}; path=${derivedPath}`;
};
Run Code Online (Sandbox Code Playgroud)

问题:

当用户注销后按浏览器的后退按钮时,就会出现此问题。似乎当用户按下后退按钮时,页面会与 cookie 一起从缓存中恢复。由于 JWT 访问令牌仍然有效,因此用户按下后退按钮后,他们仍然可以进行 api 调用并导航应用程序。

有什么办法可以解决这个问题吗?如何删除 cookie,以便按后退按钮不会恢复 cookie?我应该使用其他身份验证工作流程来解决这个问题吗?

Avi*_*ana -1

我认为这个问题可以有很多解决方案。

根据我的看法,最好的办法是如果可能的话,在服务器端使 JWT 访问令牌过期。

另一种解决方案是使用 JavaScript 禁用后退按钮。你可以使用类似的东西:

(function (global) { 
    if(typeof (global) === "undefined") {throw new Error("undefined window")}
    global.onhashchange = function () {
        if (global.location.hash !== hs) {
            global.location.hash = hs;
        }
    };
    var hs = "!";
    var BackspaceBlock = function () {
        global.location.href += "#";
        global.setTimeout(function () {
            global.location.href += "!";
        }, 50);
    };
    global.onload = function () {            
        BackspaceBlock();
        document.body.onkeydown = function (e) {
            var tm = e.target.nodeName.toLowerCase();
            if (e.key === 'Backspace' && (tm !== 'input' && tm !== 'password' && elm  !== 'textarea' && elm  !== 'email' && elm  !== 'url')) //other inputs in which you want backspace not disabled
            {
                e.preventDefault();
            }
            e.stopPropagation();
        };          
    }
})(window);
Run Code Online (Sandbox Code Playgroud)