import { useState } from 'react'; import { FaUser } from 'react-icons/fa/index'; import { motion } from 'motion/react'; import { useAuthStore } from '@/stores/auth'; import styles from './auth-button.module.css'; export function AuthButton() { const { isAuthenticated, user, login, logout, isLoading } = useAuthStore(); const [showAuthForm, setShowAuthForm] = useState(false); const [isLogin, setIsLogin] = useState(true); const [formData, setFormData] = useState({ username: '', password: '', }); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); try { if (isLogin) { await login(formData); } else { // 注册功能先用登录代替 await login(formData); } setShowAuthForm(false); setFormData({ username: '', password: '' }); } catch (error) { console.error('认证失败:', error); } }; const handleChange = (e: React.ChangeEvent) => { setFormData({ ...formData, [e.target.name]: e.target.value, }); }; const handleLogout = () => { logout(); }; const handleClick = () => { if (isAuthenticated) { // 如果已登录,显示用户菜单 return; } else { // 如果未登录,显示登录表单 setShowAuthForm(true); } }; return ( <> {isAuthenticated && user && ( )} {showAuthForm && (
setShowAuthForm(false)}>
e.stopPropagation()}>

{isLogin ? '登录' : '注册'}

)} {isAuthenticated && (
{user?.username.charAt(0).toUpperCase()}
{user?.username}
)} ); }