fix: 修复切换用户后会话列表返回错误用户数据的cookie残留问题

- 登录成功后将凭据存入sessionStorage
- req()自动附加Authorization:Basic header避免依赖易污染的cookie
- logout同步清除前端凭据和服务端cookie
This commit is contained in:
2026-09-05 16:15:41 +08:00
parent 763fc38882
commit bbacf48dcf
2 changed files with 14 additions and 2 deletions
+8 -1
View File
@@ -404,6 +404,9 @@
// ---------- API(与 core.js 的 req 保持一致) ----------
async function req(method, url, body, isForm) {
const opt = { method, headers: {} };
// 从 sessionStorage 取登录凭据,自动加 Basic auth 头,确保 cookie 残留时认证也不出错
const creds = sessionStorage.getItem('agent_creds');
if (creds) opt.headers['Authorization'] = 'Basic ' + creds;
if (body && !isForm) {
opt.headers['Content-Type'] = 'application/json';
opt.body = JSON.stringify(body);
@@ -3320,7 +3323,11 @@
window.sUI = {
// 账户
changePassword: changePassword,
logout: function () { location.href = '/logout'; },
logout: async function () {
sessionStorage.removeItem('agent_creds'); // 清凭据,避免下次访问仍走 Basic auth
try { await fetch('/api/logout', { method: 'POST' }); } catch (_) {}
location.href = '/logout';
},
// 服务
saveForm: saveForm,
restart: restart,
+6 -1
View File
@@ -87,7 +87,12 @@
throw new Error(msg);
}
const j = await res.json().catch(() => ({}));
if (j && j.token) localStorage.setItem(TOKEN_KEY, j.token);
if (j && j.token) {
localStorage.setItem(TOKEN_KEY, j.token);
// 持久化凭据供 req() 使用 Basic auth,避免 cookie 残留导致切换用户后认证错乱
const creds = btoa(encodeURIComponent((document.getElementById('login-user').value || '') + ':' + (document.getElementById('login-pass').value || '')));
sessionStorage.setItem('agent_creds', creds);
}
location.replace('/'); // 替换而非压栈:历史里不留 /login 条目,登录后侧滑不会退回登录页
} catch (e2) {
errEl.textContent = e2.message || '登录失败';