修复:多标签页切号后 /api/me 与会话列表用户不一致(身份统一 cookie 优先)
- 前端 req() 不再附加 sessionStorage 的 Basic 头:sessionStorage 按标签页隔离、刷新后残留,而 cookie 是浏览器全局,多用户切号后两者指向不同用户 - 服务端新增 authUser():cookie 优先于 Basic,中间件与 /api/me 共用同一解析顺序,杜绝显示身份与数据身份分裂;Basic 仅供 curl/脚本无 cookie 时使用 - 登录页不再往 sessionStorage 写明文口令(旧标签页残留值由 logout/启动检查顺带清理) - ARCHITECTURE.md 同步:§5.7 中间件优先级、§10.3 登录时序、§14.16 新陷阱
This commit is contained in:
+12
-3
@@ -120,6 +120,15 @@ function createAuth(cfg) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 统一身份解析:cookie 优先于 Authorization: Basic。
|
||||
// cookie 是浏览器全局的(切号登录立即覆盖、SSE 导航下载都带它),而 Basic 头可能来自
|
||||
// 旧标签页残留的 sessionStorage 凭据——两者取用时若 Basic 优先,会出现 /api/me(读 cookie)
|
||||
// 与业务路由(读 Basic)身份分裂。统一 cookie 优先保证同一浏览器内所有请求认定同一用户,
|
||||
// Basic 仅作为无 cookie 场景(curl / API 脚本)的兜底。
|
||||
function authUser(req) {
|
||||
return sessionUser(req) || basicAuthUser(req, users);
|
||||
}
|
||||
|
||||
// ----- 登录/登出 API -----
|
||||
router.post('/api/login', (req, res) => {
|
||||
const { user, password } = req.body || {};
|
||||
@@ -188,7 +197,8 @@ function createAuth(cfg) {
|
||||
});
|
||||
|
||||
router.get('/api/me', (req, res) => {
|
||||
res.json({ user: sessionUser(req) });
|
||||
// 与中间件同一解析顺序(cookie 优先),保证 /api/me 显示的用户与业务路由认定的用户一致
|
||||
res.json({ user: authUser(req) });
|
||||
});
|
||||
|
||||
// ----- 登录页 / 登出跳转 -----
|
||||
@@ -205,8 +215,7 @@ function createAuth(cfg) {
|
||||
|
||||
// ----- 全局认证中间件:cookie 或 Basic 任一通过;否则 API 回 401、页面跳登录页 -----
|
||||
const middleware = (req, res, next) => {
|
||||
const fromBasic = basicAuthUser(req, users);
|
||||
const user = fromBasic || sessionUser(req);
|
||||
const user = authUser(req);
|
||||
if (user) { req.user = user; return next(); }
|
||||
if (isPublicPath(req.path) || isSignedDownload(req)) return next();
|
||||
if (req.path.startsWith('/api/')) return res.status(401).json({ error: '未登录' });
|
||||
|
||||
@@ -427,9 +427,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;
|
||||
// 浏览器身份只认 HttpOnly cookie:服务端所有路由(含 /api/me、SSE)统一按 cookie 认定用户。
|
||||
// 不要在此附加 sessionStorage 里的 Basic 头——sessionStorage 按标签页隔离、刷新后残留,
|
||||
// 而 cookie 是浏览器全局的;两者并用会在多用户切号后出现 /api/me 是 A、列表数据是 B 的分裂。
|
||||
if (body && !isForm) {
|
||||
opt.headers['Content-Type'] = 'application/json';
|
||||
opt.body = JSON.stringify(body);
|
||||
@@ -3936,7 +3936,7 @@
|
||||
// 账户
|
||||
changePassword: changePassword,
|
||||
logout: async function () {
|
||||
sessionStorage.removeItem('agent_creds'); // 清 Basic auth 凭据
|
||||
sessionStorage.removeItem('agent_creds'); // 旧版本遗留的 Basic 凭据,顺带清理
|
||||
localStorage.removeItem('agent_token'); // 清 autoRestore token,防止自动恢复旧用户
|
||||
try { await fetch('/api/logout', { method: 'POST' }); } catch (_) {}
|
||||
location.href = '/logout';
|
||||
@@ -3987,16 +3987,15 @@
|
||||
openDrawer(drawerMQ.matches); // 桌面端:侧边栏默认展开;跨断点缩放窗口时同步切换开合态
|
||||
drawerMQ.addEventListener('change', e => openDrawer(e.matches));
|
||||
// 页面加载时校验 cookie 身份:防止 localStorage 旧 token 通过 autoRestore 恢复错误用户
|
||||
// 仅用 cookie 认证(不带 Basic auth),检测服务端实际认定的用户身份
|
||||
(async () => {
|
||||
try {
|
||||
const res = await fetch('/api/me', { credentials: 'include', headers: { Authorization: '' } });
|
||||
const res = await fetch('/api/me', { credentials: 'include' });
|
||||
if (res.ok) {
|
||||
const j = await res.json();
|
||||
if (!j.user) {
|
||||
// cookie 无效或用户已从配置中移除 → 彻底清理所有前端凭据并跳登录
|
||||
localStorage.removeItem('agent_token');
|
||||
sessionStorage.removeItem('agent_creds');
|
||||
sessionStorage.removeItem('agent_creds'); // 旧版本遗留的 Basic 凭据,顺带清理
|
||||
location.href = '/logout';
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -96,10 +96,9 @@
|
||||
}
|
||||
const j = await res.json().catch(() => ({}));
|
||||
if (j && j.token) {
|
||||
// 仅存签名 token 供 WebView 场景 login_restore 恢复;
|
||||
// 不再往 sessionStorage 写明文口令——Basic 头与 cookie 作用域不同,会引发多标签页身份分裂
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user