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, }, },