fix(auth): 修复 mixed-content 问题,将认证接口改为相对路径并走代理
This commit is contained in:
parent
95c75ce331
commit
da3fad3b44
@ -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: {
|
||||
|
||||
@ -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,
|
||||
};
|
||||
});
|
||||
|
||||
@ -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,
|
||||
},
|
||||
},
|
||||
|
||||
Loading…
Reference in New Issue
Block a user