我正在使用带有 msal 的 JWT 令牌进行用户身份验证。用户登录后,我们将 JWT 令牌存储在 localStorage 中,并在注销时清除 localStorage 以及 msal loguut。
但我想强制注销浏览器上的用户已关闭。因此,一旦浏览器关闭,我必须清除 localStorage。
我尝试为此使用 onbeforeunload 和 onunload 方法,但是在页面刷新时也会调用此方法。- 尝试使用 sessionStorage 但这会导致用户登录其选项卡特定范围的每个选项卡 cos。
我尝试了以下代码
componentDidMount() {
window.addEventListener("beforeunload",this.forceLogout,false)
}
componentWillUnmount() {
window.removeEventListener("beforeunload",this.forceLogout,false)
}
forceLogout(){
localStorage.clear();
}
注意:在 msal 登录重定向回应用程序后,由于使用了 HashRouter,我们会刷新页面