fix(auth): 支持通过环境变量配置认证地址

This commit is contained in:
肖应宇 2026-06-12 17:31:26 +08:00
parent da3fad3b44
commit c4e9a6466e
4 changed files with 16 additions and 5 deletions

View File

@ -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: {

8
src/env.d.ts vendored
View File

@ -1 +1,9 @@
/// <reference types="vite/client" />
interface ImportMetaEnv {
readonly VITE_AUTH_BASE_URL?: string
}
interface ImportMeta {
readonly env: ImportMetaEnv
}

View File

@ -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', () => {

View File

@ -22,7 +22,7 @@ export default defineConfig({
changeOrigin: true,
},
"/newapi/api": {
target: "http://test.xueai.art",
target: "https://sxwz.xueai.art",
changeOrigin: true,
},
},