'use client'; import {useEffect, useState} from 'react'; import {Alert, App, Button, Form, Input, Modal, Segmented, Typography} from 'antd'; import {LockOutlined, MobileOutlined, SafetyCertificateOutlined} from '@ant-design/icons'; import {useAuth} from '@/app/context/AuthContext'; import {useLanguage} from '@/app/context/LanguageContext'; import {accountCopy} from '@/data/account'; type LoginMode = 'otp' | 'password'; export default function AuthModal() { const auth = useAuth(); const {message} = App.useApp(); const {locale} = useLanguage(); const copy = accountCopy[locale]; const [mode, setMode] = useState('otp'); const [step, setStep] = useState<'phone' | 'code'>('phone'); const [phone, setPhone] = useState('09123456789'); const [loading, setLoading] = useState(false); const [error, setError] = useState(''); useEffect(() => { if (auth.loginOpen) { setMode('otp'); setStep('phone'); setError(''); } }, [auth.loginOpen]); const submit = async (values: {phone?: string; code?: string; password?: string}) => { setLoading(true); setError(''); try { const currentPhone = (values.phone || phone).replace(/\s/g, ''); if (mode === 'otp' && step === 'phone') { await auth.sendOtp(currentPhone); setPhone(currentPhone); setStep('code'); message.success(copy.otpSent); } else if (mode === 'otp') { await auth.verifyOtp(currentPhone, values.code || ''); } else { await auth.loginPassword(currentPhone, values.password || ''); } } catch (caught) { setError( caught instanceof Error && caught.message === 'INVALID_PHONE' ? copy.invalidPhone : copy.invalidCredentials, ); } finally { setLoading(false); } }; return ( block value={mode} onChange={value => { setMode(value); setStep('phone'); setError(''); }} options={[ {label: copy.otpLogin, value: 'otp'}, { label: copy.passwordLogin, value: 'password', }, ]} />
{copy.demoOtp}
{copy.demoPassword}
} /> {error && }
{(mode === 'password' || step === 'phone') && ( } /> )} {mode === 'password' && ( } /> )} {mode === 'otp' && step === 'code' && ( <> {copy.phone}: {phone} } /> )}
); }