From c4e9a6466e185f9158d75263c559c36cfb4f25c2 Mon Sep 17 00:00:00 2001 From: MT-Mint <798521692@qq.com> Date: Fri, 12 Jun 2026 17:31:26 +0800 Subject: [PATCH] =?UTF-8?q?fix(auth):=20=E6=94=AF=E6=8C=81=E9=80=9A?= =?UTF-8?q?=E8=BF=87=E7=8E=AF=E5=A2=83=E5=8F=98=E9=87=8F=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E8=AE=A4=E8=AF=81=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/__tests__/authStore.test.ts | 6 ++++-- src/env.d.ts | 8 ++++++++ src/stores/auth.ts | 5 +++-- vite.config.ts | 2 +- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/__tests__/authStore.test.ts b/src/__tests__/authStore.test.ts index d4e8042..adb83b1 100644 --- a/src/__tests__/authStore.test.ts +++ b/src/__tests__/authStore.test.ts @@ -6,11 +6,13 @@ describe('Auth Store', () => { setActivePinia(createPinia()) localStorage.clear() vi.clearAllMocks() + vi.unstubAllEnvs() window.history.replaceState({}, '', '/') }) - it('prefers URL token and validates it even in dev mode', async () => { + it('prefers URL token and validates it against the env-based auth url even in dev mode', async () => { window.history.replaceState({}, '', '/?token=url-token') + vi.stubEnv('VITE_AUTH_BASE_URL', 'https://sxwz.xueai.art') const fetchMock = vi.mocked(fetch) fetchMock.mockResolvedValueOnce({ @@ -38,7 +40,7 @@ describe('Auth Store', () => { expect(fetchMock).toHaveBeenCalledTimes(1) expect(fetchMock).toHaveBeenCalledWith( - '/api/auth/login/validateToken', + 'https://sxwz.xueai.art/newapi/api/login/validateToken', { method: 'POST', headers: { diff --git a/src/env.d.ts b/src/env.d.ts index 11f02fe..b12f3ca 100644 --- a/src/env.d.ts +++ b/src/env.d.ts @@ -1 +1,9 @@ /// + +interface ImportMetaEnv { + readonly VITE_AUTH_BASE_URL?: string +} + +interface ImportMeta { + readonly env: ImportMetaEnv +} diff --git a/src/stores/auth.ts b/src/stores/auth.ts index 44e92b7..6c8fe92 100644 --- a/src/stores/auth.ts +++ b/src/stores/auth.ts @@ -33,9 +33,10 @@ interface AuthResponse { [key: string]: unknown; } | null; } - + // 认证接口 -const AUTH_CHECK_URL = '/newapi/api/login/validateToken'; +const AUTH_BASE_URL = (import.meta.env.VITE_AUTH_BASE_URL || '').replace(/\/$/, ''); +const AUTH_CHECK_URL = `${AUTH_BASE_URL}/newapi/api/login/validateToken`; const AUTH_TOKEN_STORAGE_KEY = 'DEV_DEFAULT_TOKEN'; export const useAuthStore = defineStore('auth', () => { diff --git a/vite.config.ts b/vite.config.ts index 9b9e06c..ebb29ef 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -22,7 +22,7 @@ export default defineConfig({ changeOrigin: true, }, "/newapi/api": { - target: "http://test.xueai.art", + target: "https://sxwz.xueai.art", changeOrigin: true, }, },