第二版:casdoor登录,保留用户密码登录
This commit is contained in:
1 parent
a5f0ec645d
commit
61fa96d686
13 files changed
+701
-39
No files matched your search
+20
-2
@@ -4,6 +4,7 @@ import App from './App.vue'
|
||||
import router from './router'
|
||||
import i18n from './i18n'
|
||||
import { config } from './config'
|
||||
import { refreshAccessToken, getAuthToken } from './utils/auth'
|
||||
|
||||
// Set document title from config
|
||||
document.title = config.appTitle || 'AI工作台';
|
||||
@@ -11,12 +12,29 @@ document.title = config.appTitle || 'AI工作台';
|
||||
// Global 401 interceptor for API calls.
|
||||
const _rawFetch = window.fetch.bind(window);
|
||||
window.fetch = async (input: RequestInfo | URL, init?: RequestInit): Promise<Response> => {
|
||||
const res = await _rawFetch(input, init);
|
||||
// Keep original request closure so we can retry with a refreshed token.
|
||||
const runFetch = (nextInit?: RequestInit) => _rawFetch(input, nextInit);
|
||||
let reqInit = init;
|
||||
let res = await runFetch(reqInit);
|
||||
try {
|
||||
const url = typeof input === 'string'
|
||||
? input
|
||||
: (input instanceof URL ? input.toString() : input.url);
|
||||
if (res.status === 401 && url.includes('/api/')) {
|
||||
const isApi = url.includes('/api/');
|
||||
const isRefreshApi = url.includes('/api/user/casdoor/refresh');
|
||||
if (res.status === 401 && isApi && !isRefreshApi) {
|
||||
// First unauthorized response: try silent refresh once.
|
||||
const refreshed = await refreshAccessToken();
|
||||
if (refreshed) {
|
||||
const mergedHeaders = new Headers((reqInit && reqInit.headers) || {});
|
||||
mergedHeaders.set('Authorization', `Bearer ${getAuthToken()}`);
|
||||
reqInit = { ...(reqInit || {}), headers: mergedHeaders };
|
||||
// Retry original request exactly once with the renewed access token.
|
||||
res = await runFetch(reqInit);
|
||||
}
|
||||
}
|
||||
if (res.status === 401 && isApi) {
|
||||
// Still unauthorized after retry (or no refresh token): force global logout UX.
|
||||
window.dispatchEvent(new CustomEvent('hxops:unauthorized', { detail: { url, status: res.status } }));
|
||||
}
|
||||
} catch {
|
||||
|
||||
Reference in new issue
Block a user