import React, { createContext, useContext, useState, useEffect } from 'react'; import { authAPI } from '../services/api'; interface AuthContextType { isAuthenticated: boolean; user: string | null; login: (username: string, password: string) => Promise; logout: () => void; loading: boolean; } const AuthContext = createContext(undefined); export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => { const [isAuthenticated, setIsAuthenticated] = useState(false); const [user, setUser] = useState(null); const [loading, setLoading] = useState(true); // 检查是否已登录 useEffect(() => { const checkAuth = async () => { const token = localStorage.getItem('authToken'); const savedUser = localStorage.getItem('user'); if (token && savedUser) { try { // 验证令牌是否有效 await authAPI.getProfile(); setIsAuthenticated(true); setUser(savedUser); } catch (error) { // 令牌无效,清除登录状态 localStorage.removeItem('authToken'); localStorage.removeItem('user'); setIsAuthenticated(false); setUser(null); } } setLoading(false); }; checkAuth(); }, []); // 登录方法 const login = async (username: string, password: string) => { setLoading(true); try { const response = await authAPI.login(username, password); const { token, username: user } = response; // 保存令牌和用户信息到本地存储 localStorage.setItem('authToken', token); localStorage.setItem('user', user); setIsAuthenticated(true); setUser(user); } catch (error) { // 登录失败 console.error('Login failed:', error); throw error; } finally { setLoading(false); } }; // 登出方法 const logout = () => { authAPI.logout(); setIsAuthenticated(false); setUser(null); }; return ( {children} ); }; export const useAuth = () => { const context = useContext(AuthContext); if (context === undefined) { throw new Error('useAuth must be used within an AuthProvider'); } return context; };