import { useState } from 'react'; import { Box, Button, FormControl, FormLabel, Input, VStack, Heading, useToast, Text, HStack, PinInput, PinInputField } from '@chakra-ui/react'; import api from '../services/api'; type ResetStep = 'email' | 'code' | 'new_password' | 'success'; const ForgotPasswordPage: React.FC = () => { const [email, setEmail] = useState(''); const [code, setCode] = useState(''); const [newPassword, setNewPassword] = useState(''); const [confirmPassword, setConfirmPassword] = useState(''); const [isLoading, setIsLoading] = useState(false); const [step, setStep] = useState('email'); const toast = useToast(); const handleInitiateReset = async (e: React.FormEvent) => { e.preventDefault(); if (!email) return; setIsLoading(true); try { await api.post('/auth/initiate-password-reset', { email }); setStep('code'); toast({ title: 'Kód odeslán', description: 'Ověřovací kód byl odeslán na váš e-mail.', status: 'success', duration: 5000, isClosable: true, }); } catch (error: any) { toast({ title: 'Chyba', description: error.response?.data?.message || 'Nepodařilo se odeslat ověřovací kód', status: 'error', duration: 5000, isClosable: true, }); } finally { setIsLoading(false); } }; const handleVerifyCode = async (e: React.FormEvent) => { e.preventDefault(); if (code.length !== 6) return; setIsLoading(true); try { await api.post('/auth/verify-reset-code', { email, code }); setStep('new_password'); } catch (error: any) { toast({ title: 'Chyba', description: error.response?.data?.message || 'Neplatný ověřovací kód', status: 'error', duration: 5000, isClosable: true, }); } finally { setIsLoading(false); } }; const handleCompleteReset = async (e: React.FormEvent) => { e.preventDefault(); if (newPassword !== confirmPassword) { toast({ title: 'Chyba', description: 'Hesla se neshodují', status: 'error', duration: 5000, isClosable: true, }); return; } if (newPassword.length < 8) { toast({ title: 'Chyba', description: 'Heslo musí mít alespoň 8 znaků', status: 'error', duration: 5000, isClosable: true, }); return; } setIsLoading(true); try { await api.post('/auth/complete-password-reset', { email, code, new_password: newPassword, }); setStep('success'); toast({ title: 'Úspěch', description: 'Vaše heslo bylo úspěšně změněno. Nyní se můžete přihlásit.', status: 'success', duration: 5000, isClosable: true, }); } catch (error: any) { toast({ title: 'Chyba', description: error.response?.data?.message || 'Nepodařilo se změnit heslo', status: 'error', duration: 5000, isClosable: true, }); } finally { setIsLoading(false); } }; const renderStep = () => { switch (step) { case 'email': return ( Obnova hesla Zadejte e-mailovou adresu vašeho účtu. Pošleme vám ověřovací kód pro obnovu hesla. E-mail setEmail(e.target.value)} placeholder="vas@email.cz" size="lg" /> ); case 'code': return ( Ověřte svou identitu Zadejte 6místný ověřovací kód, který jsme zaslali na adresu {email} setCode(value)} isDisabled={isLoading} autoFocus otp > {[...Array(6)].map((_, i) => ( ))} ); case 'new_password': return ( Nastavení nového hesla Zadejte nové heslo pro váš účet {email} Nové heslo setNewPassword(e.target.value)} placeholder="Zadejte nové heslo" size="lg" /> Potvrzení hesla setConfirmPassword(e.target.value)} placeholder="Zadejte heslo znovu" size="lg" /> ); case 'success': return ( Vaše heslo bylo úspěšně změněno! Nyní se můžete přihlásit pomocí svého nového hesla. ); default: return null; } }; return ( {renderStep()} ); }; export default ForgotPasswordPage;