diff --git a/app/server/public/index.html b/app/server/public/index.html index 681e850..00a8939 100644 --- a/app/server/public/index.html +++ b/app/server/public/index.html @@ -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, diff --git a/app/server/public/login.html b/app/server/public/login.html index ac385b0..0f0e19f 100644 --- a/app/server/public/login.html +++ b/app/server/public/login.html @@ -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 || '登录失败';