'use client' import {useContext, useState} from "react"; import {Mail, KeyRound, Lock, ArrowLeft} from 'lucide-react'; import {QueryDataResponse} from "@/shared/interface"; import {AlertContext} from "@/context/AlertContext"; import {useTranslations} from "@/lib/i18n"; import {LangContext, LangContextProps} from "@/context/LangContext"; import {apiPostPublic} from "@/lib/api/client"; import Button from "@/components/ui/Button"; import InputField from "@/components/form/InputField"; import TextInput from "@/components/form/TextInput"; export default function ForgetPasswordPage() { const [step, setStep] = useState(1); const [email, setEmail] = useState(''); const [verificationCode, setVerificationCode] = useState(''); const [isConfirmed, setIsConfirmed] = useState(false); const [newPassword, setNewPassword] = useState(''); const {errorMessage, successMessage} = useContext(AlertContext); const t = useTranslations(); const {lang} = useContext(LangContext) async function handleEmailCheck(): Promise { if (!email) { errorMessage(t('resetPassword.error.emailInvalid')); return; } const emailRegEx = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; if (!emailRegEx.test(email)) { errorMessage(t('resetPassword.error.emailFormat')); return; } try { const response: QueryDataResponse = await apiPostPublic>('user/email-check', {email}, lang); if (response.valid) { successMessage(response.message ?? ''); setStep(2); } else { errorMessage(response.message ?? ''); } } catch (e: unknown) { if (e instanceof Error) { errorMessage(t('resetPassword.error.emailServer')); } else { errorMessage(t('resetPassword.error.emailUnknown')); } } } async function handleConfirm(): Promise { try { const response: QueryDataResponse = await apiPostPublic>('user/verify-code', { verifyCode: verificationCode, email, }, lang); if (response.valid) { successMessage(response.message ?? ''); setIsConfirmed(true); } else { errorMessage(response.message ?? ''); } } catch (e: unknown) { if (e instanceof Error) { errorMessage(t('resetPassword.error.codeServer')); } else { errorMessage(t('resetPassword.error.codeUnknown')); } } } async function handleNewPassword(): Promise { try { const response: QueryDataResponse = await apiPostPublic>('password/reset', { email, newPassword, code: verificationCode }, lang); if (response.valid) { successMessage(response.message ?? ''); setStep(3); } else { errorMessage(response.message ?? ''); } } catch (e: unknown) { if (e instanceof Error) { errorMessage(t('resetPassword.error.passwordServer')); } else { errorMessage(t('resetPassword.error.passwordUnknown')); } } } return (
ERitors

{t('resetPassword.title')}

{t('resetPassword.subtitle')}

{step < 3 && ( <>
= 1 ? 'bg-primary' : 'bg-secondary'}`}>
= 2 ? 'bg-primary' : 'bg-secondary'}`}>
= 3 ? 'bg-primary' : 'bg-secondary'}`}>
{step === 1 && ( setEmail(e.target.value)} placeholder={t('resetPassword.fields.email.placeholder')}/> }/> )} {step === 2 && ( <> setVerificationCode(e.target.value)} placeholder={t('resetPassword.fields.code.placeholder')} disabled={isConfirmed}/> }/> {isConfirmed && (

{t('resetPassword.fields.newPassword.label')}

setNewPassword(e.target.value)} required/>
)} )}
{step === 1 && ( <> )} {step === 2 && ( <> )}
)} {step === 3 && ( <>

{t('resetPassword.success')}

)}
) }