This commit is contained in:
Baobhan Sith
2025-04-14 22:51:05 +08:00
parent 286492fc63
commit a974b8b1d9
49 changed files with 13954 additions and 0 deletions
@@ -0,0 +1,82 @@
import { defineStore } from 'pinia';
import axios from 'axios';
import router from '../router'; // 引入 router 用于重定向
// 用户信息接口 (不含敏感信息)
interface UserInfo {
id: number;
username: string;
}
// Auth Store State 接口
interface AuthState {
isAuthenticated: boolean;
user: UserInfo | null;
isLoading: boolean;
error: string | null;
}
export const useAuthStore = defineStore('auth', {
state: (): AuthState => ({
isAuthenticated: false, // 初始为未登录
user: null,
isLoading: false,
error: null,
}),
getters: {
// 可以添加一些 getter,例如获取用户名
loggedInUser: (state) => state.user?.username,
},
actions: {
// 登录 Action
async login(credentials: { username: string; password: string }) {
this.isLoading = true;
this.error = null;
try {
const response = await axios.post<{ message: string; user: UserInfo }>('/api/v1/auth/login', credentials);
// 登录成功
this.isAuthenticated = true;
this.user = response.data.user;
console.log('登录成功:', this.user);
// 登录成功后重定向到连接管理页面 (或仪表盘)
await router.push({ name: 'Connections' }); // 使用 await 确保导航完成
return true;
} catch (err: any) {
console.error('登录失败:', err);
this.isAuthenticated = false;
this.user = null;
this.error = err.response?.data?.message || err.message || '登录时发生未知错误。';
return false;
} finally {
this.isLoading = false;
}
},
// 登出 Action (占位符)
async logout() {
this.isLoading = true;
this.error = null;
try {
// TODO: 调用后端的登出 API (如果需要)
// await axios.post('/api/v1/auth/logout');
// 清除本地状态
this.isAuthenticated = false;
this.user = null;
console.log('已登出');
// 登出后重定向到登录页
await router.push({ name: 'Login' });
} catch (err: any) {
console.error('登出失败:', err);
this.error = err.response?.data?.message || err.message || '登出时发生未知错误。';
} finally {
this.isLoading = false;
}
},
// TODO: 添加检查登录状态的 Action (例如应用启动时调用)
// async checkAuthStatus() { ... }
},
// 可选:开启持久化 (例如使用 pinia-plugin-persistedstate)
// persist: true,
});