import React, { useState, useEffect } from 'react' import { useAuth } from '../contexts/AuthContext' import { useLanguage } from '../contexts/LanguageContext' import { t } from '../i18n/translations' import { Eye, EyeOff } from 'lucide-react' import { DeepVoidBackground } from './DeepVoidBackground' // import { Input } from './ui/input' // Removed unused import import { toast } from 'sonner' import { useSystemConfig } from '../hooks/useSystemConfig' export function LoginPage() { const { language } = useLanguage() const { login, loginAdmin, verifyOTP } = useAuth() 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 { config: systemConfig } = useSystemConfig() const registrationEnabled = systemConfig?.registration_enabled !== false const [expiredToastId, setExpiredToastId] = useState(null) // Show notification if user was redirected here due to 401 useEffect(() => { if (sessionStorage.getItem('from401') === 'true') { const id = toast.warning(t('sessionExpired', language), { duration: Infinity // Keep showing until user dismisses or logs in }) setExpiredToastId(id) sessionStorage.removeItem('from401') } }, [language]) 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) } else { // Dismiss the "login expired" toast on successful login if (expiredToastId) { toast.dismiss(expiredToastId) } } 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 { // Dismiss the "login expired" toast on successful login (no OTP required) if (expiredToastId) { toast.dismiss(expiredToastId) } } } 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) } else { // Dismiss the "login expired" toast on successful OTP verification if (expiredToastId) { toast.dismiss(expiredToastId) } } // 成功的话AuthContext会自动处理登录状态 setLoading(false) } return (
{/* Navigation - Top Bar (Mobile/Desktop Friendly) */}
{/* Terminal Header */}
NoFx Logo

SYSTEM ACCESS

{step === 'login' ? 'Authentication Protocol v3.0' : 'Multi-Factor Verification'}

{/* Terminal Output / Form Container */}
{/* Window Bar */}
window.location.href = '/'} title="Close / Return Home" >
login.exe
{/* Status Output */}
Initiating handshake...
Target: NOFX CORE HUB
Status: AWAITING CREDENTIALS
{adminMode ? (
setAdminPassword(e.target.value)} className="w-full bg-black/50 border border-zinc-700 rounded px-4 py-3 text-sm focus:border-nofx-gold focus:ring-1 focus:ring-nofx-gold/50 outline-none transition-all placeholder-zinc-700 text-white font-mono" placeholder="ENTER_ROOT_PASSWORD" required />
{error && (
[ERROR]: {error}
)}
) : step === 'login' ? (
setEmail(e.target.value)} className="w-full bg-black/50 border border-zinc-700 rounded px-4 py-3 text-sm focus:border-nofx-gold focus:ring-1 focus:ring-nofx-gold/50 outline-none transition-all placeholder-zinc-700 text-white font-mono" placeholder="user@nofx.os" required />
setPassword(e.target.value)} className="w-full bg-black/50 border border-zinc-700 rounded px-4 py-3 text-sm focus:border-nofx-gold focus:ring-1 focus:ring-nofx-gold/50 outline-none transition-all placeholder-zinc-700 text-white font-mono pr-10" placeholder="••••••••••••" required />
{error && (
{error}
)}
) : (
🔐

{t('scanQRCodeInstructions', language)}
{t('enterOTPCode', language)}

setOtpCode(e.target.value.replace(/\D/g, '').slice(0, 6)) } className="w-full bg-black border border-zinc-700 rounded px-4 py-4 text-center text-2xl tracking-[0.5em] font-mono text-white focus:border-nofx-gold focus:ring-1 focus:ring-nofx-gold/50 outline-none transition-all placeholder-zinc-800" placeholder="000000" maxLength={6} required autoFocus />
{error && (
[ACCESS DENIED]: {error}
)}
)}
{/* Terminal Footer Info */}
SECURE_CONNECTION: ENCRYPTED
{new Date().toISOString().split('T')[0]}
{/* Register Link */} {!adminMode && registrationEnabled && (

NEW_USER_DETECTED?{' '}

)}
) }