From fec5a321fdc6aad23ead6839e36c89a03221af8b Mon Sep 17 00:00:00 2001 From: sgp Date: Wed, 4 Nov 2026 18:49:04 +0800 Subject: [PATCH] =?UTF-8?q?chore(auth):=20=E6=9C=AA=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E6=97=B6=20/auth/me=20=E8=BF=94=E5=9B=9E=20loggedIn:false?= =?UTF-8?q?=EF=BC=8C=E9=81=BF=E5=85=8D=E7=99=BB=E5=BD=95=E9=A1=B5=E6=8E=A7?= =?UTF-8?q?=E5=88=B6=E5=8F=B0=20401=20=E5=99=AA=E9=9F=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/server.js | 6 +++--- login.html | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/api/server.js b/api/server.js index 4b2f784..5a39434 100644 --- a/api/server.js +++ b/api/server.js @@ -432,11 +432,11 @@ async function handleAuth(req, res, pathname) { }); } - // 当前登录态 + // 当前登录态(未登录返回 loggedIn:false,避免前端控制台出现 401 噪音) if (pathname === '/api/v1/auth/me' && method === 'GET') { const s = currentSession(req); - if (!s) return fail(res, 401, 'UNAUTHORIZED', '未登录'); - return ok(res, { username: s.u, expiresAt: new Date(s.exp).toISOString() }); + if (!s) return ok(res, { loggedIn: false }); + return ok(res, { loggedIn: true, username: s.u, expiresAt: new Date(s.exp).toISOString() }); } // nginx auth_request 子请求:200 = 放行,401 = 跳登录 diff --git a/login.html b/login.html index 236ace2..920087c 100644 --- a/login.html +++ b/login.html @@ -284,10 +284,10 @@ a { color: inherit; text-decoration: none; } return '/home.html'; } - // 已登录则直接跳转 + // 已登录则直接跳转(未登录时接口返回 loggedIn:false,不产生控制台报错) fetch(API_BASE + '/api/v1/auth/me', { credentials: 'include' }) - .then(function (r) { return r.ok ? r.json() : Promise.reject(); }) - .then(function () { location.replace(nextUrl()); }) + .then(function (r) { return r.json(); }) + .then(function (d) { if (d && d.ok && d.data && d.data.loggedIn) location.replace(nextUrl()); }) .catch(function () { /* 未登录,停留 */ }); passEl.addEventListener('input', function () { setMsg(''); });