import React, { useState, useEffect } from 'react'; import { useSearchParams, useNavigate } from 'react-router-dom'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Alert, AlertDescription } from '@/components/ui/alert'; import { Loader2, CheckCircle, AlertCircle, Eye, EyeOff, Lock } from 'lucide-react'; import { __ } from '@/lib/i18n'; export default function ResetPassword() { const [searchParams] = useSearchParams(); const navigate = useNavigate(); const key = searchParams.get('key') || ''; const login = searchParams.get('login') || ''; const [password, setPassword] = useState(''); const [confirmPassword, setConfirmPassword] = useState(''); const [showPassword, setShowPassword] = useState(false); const [isLoading, setIsLoading] = useState(false); const [isValidating, setIsValidating] = useState(true); const [isValid, setIsValid] = useState(false); const [error, setError] = useState(''); const [success, setSuccess] = useState(false); // Validate the reset key on mount useEffect(() => { const validateKey = async () => { if (!key || !login) { setError(__('Invalid password reset link. Please request a new one.')); setIsValidating(false); return; } try { const response = await fetch(`${window.WNW_CONFIG?.restUrl || '/wp-json/'}woonoow/v1/auth/validate-reset-key`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ key, login }), }); const data = await response.json(); if (response.ok && data.valid) { setIsValid(true); } else { setError(data.message || __('This password reset link has expired or is invalid. Please request a new one.')); } } catch (err) { setError(__('Unable to validate reset link. Please try again later.')); } finally { setIsValidating(false); } }; validateKey(); }, [key, login]); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(''); // Validate passwords match if (password !== confirmPassword) { setError(__('Passwords do not match')); return; } // Validate password strength if (password.length < 8) { setError(__('Password must be at least 8 characters long')); return; } setIsLoading(true); try { const response = await fetch(`${window.WNW_CONFIG?.restUrl || '/wp-json/'}woonoow/v1/auth/reset-password`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ key, login, password }), }); const data = await response.json(); if (response.ok) { setSuccess(true); } else { setError(data.message || __('Failed to reset password. Please try again.')); } } catch (err) { setError(__('An error occurred. Please try again later.')); } finally { setIsLoading(false); } }; // Password strength indicator const getPasswordStrength = (pwd: string) => { if (pwd.length === 0) return { label: '', color: '' }; if (pwd.length < 8) return { label: __('Too short'), color: 'text-red-500' }; let strength = 0; if (pwd.length >= 8) strength++; if (pwd.length >= 12) strength++; if (/[a-z]/.test(pwd) && /[A-Z]/.test(pwd)) strength++; if (/\d/.test(pwd)) strength++; if (/[!@#$%^&*(),.?":{}|<>]/.test(pwd)) strength++; if (strength <= 2) return { label: __('Weak'), color: 'text-orange-500' }; if (strength <= 3) return { label: __('Medium'), color: 'text-yellow-500' }; return { label: __('Strong'), color: 'text-green-500' }; }; const passwordStrength = getPasswordStrength(password); // Loading state if (isValidating) { return (
{__('Validating reset link...')}
{__('Your password has been updated. You can now log in with your new password.')}
{error}