import React, { useState, useEffect } from 'react'; import { useForm } from 'react-hook-form'; import { yupResolver } from '@hookform/resolvers/yup'; import { X, Mail, ArrowLeft, Send, Loader2, CheckCircle } from 'lucide-react'; import useAuthStore from '../../../store/useAuthStore'; import { forgotPasswordSchema, ForgotPasswordFormData } from '../../../shared/utils/validationSchemas'; import { useCompanySettings } from '../../../shared/contexts/CompanySettingsContext'; import { useAuthModal } from '../contexts/AuthModalContext'; import { useAntibotForm } from '../hooks/useAntibotForm'; import HoneypotField from '../../../shared/components/HoneypotField'; import { toast } from 'react-toastify'; const ForgotPasswordModal: React.FC = () => { const { closeModal, openModal } = useAuthModal(); const { forgotPassword, isLoading, error, clearError } = useAuthStore(); const { settings } = useCompanySettings(); const [isSuccess, setIsSuccess] = useState(false); const [submittedEmail, setSubmittedEmail] = useState(''); // Enhanced antibot protection const { honeypotValue, setHoneypotValue, validate: validateAntibot, rateLimitInfo, } = useAntibotForm({ formId: 'forgot-password', minTimeOnPage: 3000, minTimeToFill: 2000, requireRecaptcha: false, maxAttempts: 3, onValidationError: (errors) => { errors.forEach((err) => toast.error(err)); }, }); const supportEmail = settings.company_email || 'support@hotel.com'; const supportPhone = settings.company_phone || '1900-xxxx'; const { register, handleSubmit, formState: { errors }, } = useForm({ resolver: yupResolver(forgotPasswordSchema), defaultValues: { email: '', }, }); const onSubmit = async (data: ForgotPasswordFormData) => { try { clearError(); // Validate antibot protection const isValid = await validateAntibot(); if (!isValid) { return; } setSubmittedEmail(data.email); await forgotPassword({ email: data.email }); setIsSuccess(true); } catch (error) { console.error('Forgot password error:', error); } }; // Handle escape key useEffect(() => { const handleEscape = (e: KeyboardEvent) => { if (e.key === 'Escape') { closeModal(); } }; document.addEventListener('keydown', handleEscape); return () => document.removeEventListener('keydown', handleEscape); }, [closeModal]); return (
{ if (e.target === e.currentTarget) { closeModal(); } }} > {/* Backdrop */}
{/* Modal */}
{/* Close button */}
{/* Header */}
{settings.company_logo_url ? ( {settings.company_name ) : (
)}
{settings.company_tagline && (

{settings.company_tagline}

)}

Forgot Password?

Enter your email to receive a password reset link for {settings.company_name || 'Luxury Hotel'}

{isSuccess ? (

Email Sent!

We have sent a password reset link to

{submittedEmail}

Note:

  • Link is valid for 1 hour
  • Check your Spam/Junk folder
  • If you don't receive the email, please try again
) : (
{/* Honeypot field - hidden from users */} {rateLimitInfo && !rateLimitInfo.allowed && (
Too many password reset attempts. Please try again later.
)} {error && (
{error}
)}
{errors.email && (

{errors.email.message}

)}
)} {!isSuccess && (

Don't have an account?{' '}

)} {!isSuccess && (

Need Help?

If you're having trouble resetting your password, please contact our support team via email{' '} {supportEmail} {supportPhone && ( <> {' '}or hotline{' '} {supportPhone} )}

)}
); }; export default ForgotPasswordModal;