fix: 修复切换用户后会话列表返回错误用户数据
根因:cookie残留/污染 + localStorage autoRestore 自动恢复旧用户 - 浏览器清cookie不会清localStorage,autoRestore用旧token自动恢复登录 - 前端req()未发Authorization头,完全依赖cookie认证 - 当cookie失效时req.user为空,后端不过滤会话返回所有数据 修复: - 登录成功后存凭据到sessionStorage,req()自动加Basic auth头 - logout彻底清理:sessionStorage + localStorage + POST /api/logout - autoRestore失败时清除无效token,成功时续期token - 退出跳转带logged_out参数,login页兜底清除所有凭据
This commit is contained in:
@@ -3324,7 +3324,8 @@
|
||||
// 账户
|
||||
changePassword: changePassword,
|
||||
logout: async function () {
|
||||
sessionStorage.removeItem('agent_creds'); // 清凭据,避免下次访问仍走 Basic auth
|
||||
sessionStorage.removeItem('agent_creds'); // 清 Basic auth 凭据
|
||||
localStorage.removeItem('agent_token'); // 清 autoRestore token,防止自动恢复旧用户
|
||||
try { await fetch('/api/logout', { method: 'POST' }); } catch (_) {}
|
||||
location.href = '/logout';
|
||||
},
|
||||
|
||||
@@ -53,6 +53,7 @@
|
||||
async function autoRestore() {
|
||||
if (new URLSearchParams(location.search).has('logged_out')) {
|
||||
localStorage.removeItem(TOKEN_KEY); // 显式登出:不恢复,并清掉旧 token
|
||||
sessionStorage.removeItem('agent_creds');
|
||||
return;
|
||||
}
|
||||
const t = localStorage.getItem(TOKEN_KEY);
|
||||
@@ -61,9 +62,16 @@
|
||||
form.style.visibility = 'hidden'; // 恢复期间先藏表单,避免闪现
|
||||
try {
|
||||
const res = await fetch('/api/login_restore', { method: 'POST', headers: { Authorization: 'Bearer ' + t } });
|
||||
if (res.ok) { location.replace('/'); return; }
|
||||
if (res.ok) {
|
||||
const j = await res.json().catch(() => ({}));
|
||||
if (j && j.token) localStorage.setItem(TOKEN_KEY, j.token); // 续期 token
|
||||
location.replace('/');
|
||||
return;
|
||||
}
|
||||
} catch (_) { /* 网络错误交给表单正常登录 */ }
|
||||
// 恢复失败(401等):清掉无效 token,显示登录表单
|
||||
localStorage.removeItem(TOKEN_KEY);
|
||||
sessionStorage.removeItem('agent_creds');
|
||||
form.style.visibility = '';
|
||||
}
|
||||
async function doLogin(e) {
|
||||
|
||||
Reference in New Issue
Block a user