fix(auth): 修改密码当新密码与旧密码相同时弹出错误,不再退出登录

This commit is contained in:
sophie committed 2026-09-08 13:32:39 +08:00
1 parent 9570197378
commit 0f8b318458
18 files changed
+247 -18

No files matched your search

+30 -9
View File
@@ -2,7 +2,7 @@
* main.ts
* 用途:Vue 应用启动入口(pinia / router / i18n / 401 fetch 包装)。
* 依赖:App.vue、router、i18n、config、auth sync、agentTabs。
* 逻辑:挂载前设置 document.title;包装 fetch 以刷新 token 并派发未授权事件。
* 逻辑:挂载前设置 document.title;包装 fetch 以刷新 token;仅会话类 401 才派发未授权事件。
*/
import { createApp } from 'vue';
import './style.css';
@@ -12,13 +12,28 @@ import i18n from './i18n';
import { config } from './config';
import { pinia } from './app/plugins/pinia';
import { bindAuthStoreSync } from './features/auth/stores/auth.store';
import { refreshAccessToken, getAuthToken, initAuthSync } from '@/shared/utils/auth';
import { refreshAccessToken, getAuthToken, initAuthSync, isSessionUnauthorizedError } from '@/shared/utils/auth';
import { initAgentTabSync } from '@/shared/utils/agentTabs';
initAuthSync();
initAgentTabSync();
document.title = String(i18n.global.t(config.appTitleKey));
/**
* 从 401 响应 envelope 读取 error.code,供会话失效判断使用。
* 核心逻辑:clone 后再 json,避免消耗原 Response body。
* @param res - fetch 原始响应
* @returns 错误码;无法解析时为空字符串
*/
async function readApiErrorCode(res: Response): Promise<string> {
try {
const body = await res.clone().json();
return String(body?.error?.code || body?.code || '').trim();
} catch {
return '';
}
}
const _rawFetch = window.fetch.bind(window);
window.fetch = async (input: RequestInfo | URL, init?: RequestInit): Promise<Response> => {
const runFetch = (nextInit?: RequestInit) => _rawFetch(input, nextInit);
@@ -34,16 +49,22 @@ window.fetch = async (input: RequestInfo | URL, init?: RequestInit): Promise<Res
url.includes('/api/user/casdoor/login');
const isOptionalSsoCatalogApi = /\/api\/sso\/apps(?:\?|$)/.test(url);
if (res.status === 401 && isApi && !isRefreshApi) {
const refreshed = await refreshAccessToken();
if (refreshed) {
const mergedHeaders = new Headers((reqInit && reqInit.headers) || {});
mergedHeaders.set('Authorization', `Bearer ${getAuthToken()}`);
reqInit = { ...(reqInit || {}), headers: mergedHeaders };
res = await runFetch(reqInit);
const errorCode = await readApiErrorCode(res);
if (isSessionUnauthorizedError(errorCode)) {
const refreshed = await refreshAccessToken();
if (refreshed) {
const mergedHeaders = new Headers((reqInit && reqInit.headers) || {});
mergedHeaders.set('Authorization', `Bearer ${getAuthToken()}`);
reqInit = { ...(reqInit || {}), headers: mergedHeaders };
res = await runFetch(reqInit);
}
}
}
if (res.status === 401 && isApi && !isLoginApi && !isOptionalSsoCatalogApi) {
window.dispatchEvent(new CustomEvent('hxops:unauthorized', { detail: { url, status: res.status } }));
const errorCode = await readApiErrorCode(res);
if (isSessionUnauthorizedError(errorCode)) {
window.dispatchEvent(new CustomEvent('hxops:unauthorized', { detail: { url, status: res.status } }));
}
}
} catch {
// no-op