From da3fad3b44b2f5decc214658471b7bd0a1a8111d Mon Sep 17 00:00:00 2001 From: MT-Mint <798521692@qq.com> Date: Fri, 12 Jun 2026 14:30:33 +0800 Subject: [PATCH] =?UTF-8?q?fix(auth):=20=E4=BF=AE=E5=A4=8D=20mixed-content?= =?UTF-8?q?=20=E9=97=AE=E9=A2=98=EF=BC=8C=E5=B0=86=E8=AE=A4=E8=AF=81?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E6=94=B9=E4=B8=BA=E7=9B=B8=E5=AF=B9=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E5=B9=B6=E8=B5=B0=E4=BB=A3=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/__tests__/authStore.test.ts | 2 +- src/stores/auth.ts | 132 ++++++++++++++++---------------- vite.config.ts | 4 +- 3 files changed, 69 insertions(+), 69 deletions(-) diff --git a/src/__tests__/authStore.test.ts b/src/__tests__/authStore.test.ts index f65428b..d4e8042 100644 --- a/src/__tests__/authStore.test.ts +++ b/src/__tests__/authStore.test.ts @@ -38,7 +38,7 @@ describe('Auth Store', () => { expect(fetchMock).toHaveBeenCalledTimes(1) expect(fetchMock).toHaveBeenCalledWith( - 'http://test.xueai.art/newapi/api/login/validateToken', + '/api/auth/login/validateToken', { method: 'POST', headers: { diff --git a/src/stores/auth.ts b/src/stores/auth.ts index 7ce5cb7..44e92b7 100644 --- a/src/stores/auth.ts +++ b/src/stores/auth.ts @@ -1,6 +1,6 @@ -/** - * 用户认证状态管理 - */ +/** + * 用户认证状态管理 + */ import { defineStore } from 'pinia'; import { ref, computed } from 'vue'; import type { UserInfo } from '@/types/chat'; @@ -15,7 +15,7 @@ const DEV_BYPASS_USER: UserInfo = { username: 'dev-user', nickname: '开发环境用户', }; - + // 认证接口返回格式 interface AuthResponse { status: number; @@ -35,9 +35,9 @@ interface AuthResponse { } // 认证接口 -const AUTH_CHECK_URL = 'http://test.xueai.art/newapi/api/login/validateToken'; +const AUTH_CHECK_URL = '/newapi/api/login/validateToken'; const AUTH_TOKEN_STORAGE_KEY = 'DEV_DEFAULT_TOKEN'; - + export const useAuthStore = defineStore('auth', () => { // 状态 const token = ref(null); @@ -47,10 +47,10 @@ export const useAuthStore = defineStore('auth', () => { // 计算属性 const isAuthenticated = computed(() => DEV_AUTH_BYPASS || !!token.value); const userId = computed(() => user.value?.username || null); // username 用于 OSS 路径和数据库 user_id - - /** - * 验证 token 并获取用户信息 - */ + + /** + * 验证 token 并获取用户信息 + */ async function checkToken(tokenToCheck: string): Promise { try { const response = await fetch(AUTH_CHECK_URL, { @@ -78,16 +78,16 @@ export const useAuthStore = defineStore('auth', () => { window.$toast?.('[Auth] Token 验证失败:Token无效'); } return null; - } catch (error) { - - console.error('[Auth] Token 验证失败:', error); - return null; - } - } - - /** - * 初始化 - 从 URL 参数获取 token,验证后设置用户信息 - */ + } catch (error) { + + console.error('[Auth] Token 验证失败:', error); + return null; + } + } + + /** + * 初始化 - 从 URL 参数获取 token,验证后设置用户信息 + */ async function init() { const searchParams = new URLSearchParams(window.location.search); const urlToken = searchParams.get('token'); @@ -109,51 +109,51 @@ export const useAuthStore = defineStore('auth', () => { window.$toast?.('未登录,请先登录', 'error'); return; } - - // 验证 token - const userInfo = await checkToken(tokenValue); - - if (userInfo) { - - token.value = tokenValue; - user.value = userInfo; - } else { - // 验证失败,清空 - token.value = null; - user.value = null; - } - - isInitialized.value = true; - } - - /** - * 设置用户信息 - */ - function setUser(userInfo: UserInfo) { - user.value = userInfo; - } - - /** - * 获取认证 header - */ - function getAuthHeader(): Record { - if (token.value) { - return { Authorization: `Bearer ${token.value}` }; - } - return {}; - } - + + // 验证 token + const userInfo = await checkToken(tokenValue); + + if (userInfo) { + + token.value = tokenValue; + user.value = userInfo; + } else { + // 验证失败,清空 + token.value = null; + user.value = null; + } + + isInitialized.value = true; + } + + /** + * 设置用户信息 + */ + function setUser(userInfo: UserInfo) { + user.value = userInfo; + } + + /** + * 获取认证 header + */ + function getAuthHeader(): Record { + if (token.value) { + return { Authorization: `Bearer ${token.value}` }; + } + return {}; + } + return { - // 状态 - token, - user, - isAuthenticated, - userId, - isInitialized, - - // 方法 - setUser, - getAuthHeader, - init, - }; + // 状态 + token, + user, + isAuthenticated, + userId, + isInitialized, + + // 方法 + setUser, + getAuthHeader, + init, + }; }); diff --git a/vite.config.ts b/vite.config.ts index 6de7e60..9b9e06c 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -21,8 +21,8 @@ export default defineConfig({ target: "http://localhost:8002", // Python服务器端口 changeOrigin: true, }, - "/api/auth": { - target: "https://sxwz.xueai.art", + "/newapi/api": { + target: "http://test.xueai.art", changeOrigin: true, }, },