import React, { useState } from 'react' import { useNavigate } from 'react-router-dom' import { useAuth } from '../contexts/AuthContext' import { useLanguage } from '../contexts/LanguageContext' import { t } from '../i18n/translations' import { Eye, EyeOff } from 'lucide-react' import { Input } from './ui/input' import { toast } from 'sonner' export function LoginPage() { const { language } = useLanguage() const { login, loginAdmin, verifyOTP } = useAuth() const navigate = useNavigate() const [step, setStep] = useState<'login' | 'otp'>('login') const [email, setEmail] = useState('') const [password, setPassword] = useState('') const [showPassword, setShowPassword] = useState(false) const [otpCode, setOtpCode] = useState('') const [userID, setUserID] = useState('') const [error, setError] = useState('') const [loading, setLoading] = useState(false) const [adminPassword, setAdminPassword] = useState('') const adminMode = false const handleAdminLogin = async (e: React.FormEvent) => { e.preventDefault() setError('') setLoading(true) const result = await loginAdmin(adminPassword) if (!result.success) { const msg = result.message || t('loginFailed', language) setError(msg) toast.error(msg) } setLoading(false) } const handleLogin = async (e: React.FormEvent) => { e.preventDefault() setError('') setLoading(true) const result = await login(email, password) if (result.success) { if (result.requiresOTP && result.userID) { setUserID(result.userID) setStep('otp') } } else { const msg = result.message || t('loginFailed', language) setError(msg) toast.error(msg) } setLoading(false) } const handleOTPVerify = async (e: React.FormEvent) => { e.preventDefault() setError('') setLoading(true) const result = await verifyOTP(userID, otpCode) if (!result.success) { const msg = result.message || t('verificationFailed', language) setError(msg) toast.error(msg) } // 成功的话AuthContext会自动处理登录状态 setLoading(false) } return (
{step === 'login' ? '请输入您的邮箱和密码' : '请输入两步验证码'}
还没有账户?{' '}