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).toHaveBeenCalledTimes(1)
|
||||||
expect(fetchMock).toHaveBeenCalledWith(
|
expect(fetchMock).toHaveBeenCalledWith(
|
||||||
'http://test.xueai.art/newapi/api/login/validateToken',
|
'/api/auth/login/validateToken',
|
||||||
{
|
{
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
/**
|
/**
|
||||||
* 用户认证状态管理
|
* 用户认证状态管理
|
||||||
*/
|
*/
|
||||||
import { defineStore } from 'pinia';
|
import { defineStore } from 'pinia';
|
||||||
import { ref, computed } from 'vue';
|
import { ref, computed } from 'vue';
|
||||||
import type { UserInfo } from '@/types/chat';
|
import type { UserInfo } from '@/types/chat';
|
||||||
@ -15,7 +15,7 @@ const DEV_BYPASS_USER: UserInfo = {
|
|||||||
username: 'dev-user',
|
username: 'dev-user',
|
||||||
nickname: '开发环境用户',
|
nickname: '开发环境用户',
|
||||||
};
|
};
|
||||||
|
|
||||||
// 认证接口返回格式
|
// 认证接口返回格式
|
||||||
interface AuthResponse {
|
interface AuthResponse {
|
||||||
status: number;
|
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';
|
const AUTH_TOKEN_STORAGE_KEY = 'DEV_DEFAULT_TOKEN';
|
||||||
|
|
||||||
export const useAuthStore = defineStore('auth', () => {
|
export const useAuthStore = defineStore('auth', () => {
|
||||||
// 状态
|
// 状态
|
||||||
const token = ref<string | null>(null);
|
const token = ref<string | null>(null);
|
||||||
@ -47,10 +47,10 @@ export const useAuthStore = defineStore('auth', () => {
|
|||||||
// 计算属性
|
// 计算属性
|
||||||
const isAuthenticated = computed(() => DEV_AUTH_BYPASS || !!token.value);
|
const isAuthenticated = computed(() => DEV_AUTH_BYPASS || !!token.value);
|
||||||
const userId = computed(() => user.value?.username || null); // username 用于 OSS 路径和数据库 user_id
|
const userId = computed(() => user.value?.username || null); // username 用于 OSS 路径和数据库 user_id
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 验证 token 并获取用户信息
|
* 验证 token 并获取用户信息
|
||||||
*/
|
*/
|
||||||
async function checkToken(tokenToCheck: string): Promise<UserInfo | null> {
|
async function checkToken(tokenToCheck: string): Promise<UserInfo | null> {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(AUTH_CHECK_URL, {
|
const response = await fetch(AUTH_CHECK_URL, {
|
||||||
@ -78,16 +78,16 @@ export const useAuthStore = defineStore('auth', () => {
|
|||||||
window.$toast?.('[Auth] Token 验证失败:Token无效');
|
window.$toast?.('[Auth] Token 验证失败:Token无效');
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
||||||
console.error('[Auth] Token 验证失败:', error);
|
console.error('[Auth] Token 验证失败:', error);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 初始化 - 从 URL 参数获取 token,验证后设置用户信息
|
* 初始化 - 从 URL 参数获取 token,验证后设置用户信息
|
||||||
*/
|
*/
|
||||||
async function init() {
|
async function init() {
|
||||||
const searchParams = new URLSearchParams(window.location.search);
|
const searchParams = new URLSearchParams(window.location.search);
|
||||||
const urlToken = searchParams.get('token');
|
const urlToken = searchParams.get('token');
|
||||||
@ -109,51 +109,51 @@ export const useAuthStore = defineStore('auth', () => {
|
|||||||
window.$toast?.('未登录,请先登录', 'error');
|
window.$toast?.('未登录,请先登录', 'error');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 验证 token
|
// 验证 token
|
||||||
const userInfo = await checkToken(tokenValue);
|
const userInfo = await checkToken(tokenValue);
|
||||||
|
|
||||||
if (userInfo) {
|
if (userInfo) {
|
||||||
|
|
||||||
token.value = tokenValue;
|
token.value = tokenValue;
|
||||||
user.value = userInfo;
|
user.value = userInfo;
|
||||||
} else {
|
} else {
|
||||||
// 验证失败,清空
|
// 验证失败,清空
|
||||||
token.value = null;
|
token.value = null;
|
||||||
user.value = null;
|
user.value = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
isInitialized.value = true;
|
isInitialized.value = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置用户信息
|
* 设置用户信息
|
||||||
*/
|
*/
|
||||||
function setUser(userInfo: UserInfo) {
|
function setUser(userInfo: UserInfo) {
|
||||||
user.value = userInfo;
|
user.value = userInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取认证 header
|
* 获取认证 header
|
||||||
*/
|
*/
|
||||||
function getAuthHeader(): Record<string, string> {
|
function getAuthHeader(): Record<string, string> {
|
||||||
if (token.value) {
|
if (token.value) {
|
||||||
return { Authorization: `Bearer ${token.value}` };
|
return { Authorization: `Bearer ${token.value}` };
|
||||||
}
|
}
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
// 状态
|
// 状态
|
||||||
token,
|
token,
|
||||||
user,
|
user,
|
||||||
isAuthenticated,
|
isAuthenticated,
|
||||||
userId,
|
userId,
|
||||||
isInitialized,
|
isInitialized,
|
||||||
|
|
||||||
// 方法
|
// 方法
|
||||||
setUser,
|
setUser,
|
||||||
getAuthHeader,
|
getAuthHeader,
|
||||||
init,
|
init,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|||||||
@ -21,8 +21,8 @@ export default defineConfig({
|
|||||||
target: "http://localhost:8002", // Python服务器端口
|
target: "http://localhost:8002", // Python服务器端口
|
||||||
changeOrigin: true,
|
changeOrigin: true,
|
||||||
},
|
},
|
||||||
"/api/auth": {
|
"/newapi/api": {
|
||||||
target: "https://sxwz.xueai.art",
|
target: "http://test.xueai.art",
|
||||||
changeOrigin: true,
|
changeOrigin: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user