fix(auth): 修复 mixed-content 问题,将认证接口改为相对路径并走代理

This commit is contained in:
肖应宇 2026-06-12 14:30:33 +08:00
parent 95c75ce331
commit da3fad3b44
3 changed files with 69 additions and 69 deletions

View File

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

View File

@ -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<string | null>(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<UserInfo | null> {
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<string, string> {
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<string, string> {
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,
};
});

View File

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