feat(auth): implement login and user management features

- Added server-side login action with form handling and cookie storage
- Implemented user authentication service with token management
- Created user list page with data fetching from userService
- Developed reusable DataTable component with selection and pagination
- Enhanced AppSidebar with nested navigation and active state tracking
- Updated icon definitions and sprite symbols for UI consistency
- Improved HTTP client to properly handle request bodies for different methods
- Refactored auth store to manage authentication state and cookies
- Added strict typing for navigation items and table columns
- Removed obsolete code and simplified authentication flow
This commit is contained in:
Chaos
2025-11-24 17:11:41 +08:00
parent 3515faa814
commit ed542f108c
16 changed files with 472 additions and 203 deletions

View File

@@ -3,7 +3,6 @@ import type { AuthResponse, LoginPayload } from '$lib/types/auth';
import { authStore } from '$lib/stores/authStore';
import { userService } from '$lib/api/services/userService';
import { toast } from '$lib/stores/toastStore';
import { get } from 'svelte/store';
export const authService = {
/**
@@ -19,15 +18,11 @@ export const authService = {
const { token, tokenHead } = response.data;
// 2. 临时设置 Token 到 Store
// 这一步是必须的,因为接下来的 userService.getUserProfile()
// 里的 API 请求拦截器需要读取 Store 中的 Token 才能通过鉴权。
// 我们先以“部分登录”的状态更新 Store。
authStore.update(s => ({ ...s, token, tokenHead, isAuthenticated: true }));
try {
// 3. 获取用户信息
const userProfile = await userService.getUserProfile();
const userProfile = await userService.getUserProfile({tokenHead,token});
// 4. 最终确认登录状态(更新完整信息并持久化)
// 这里调用 Store 封装好的 login 方法,它会负责写入 localStorage
@@ -40,9 +35,7 @@ export const authService = {
return response.data;
} catch (error) {
// 5. 安全回滚
// 如果获取用户信息失败(比如 Token 虽然返回了但无效,或者网络波动),
// 我们应该立即清除刚才设置的临时 Token防止应用处于中间状态。
console.error('获取用户信息失败,回滚登录状态', error);
authStore.logout();
throw error; // 继续抛出错误给 UI 层处理
@@ -53,10 +46,7 @@ export const authService = {
* 登出流程
*/
logout: async () => {
// 逻辑大大简化:只负责调用 Store 和 UI 反馈
authStore.logout();
toast.success('退出登录成功');
// 如果需要调用后端登出接口(使 Token 失效),在这里 await api.post('/auth/logout')
}
};