import React, { useState } from 'react'; import { authService } from '../../services/auth/auth.service'; import { useUser } from '../../contexts/UserContext'; interface ChangePasswordModalProps { onClose: () => void; onSuccess: () => void; } const ChangePasswordModal: React.FC = ({ onClose, onSuccess, }) => { const { user } = useUser(); const [currentPassword, setCurrentPassword] = useState(''); const [newPassword, setNewPassword] = useState(''); const [confirmPassword, setConfirmPassword] = useState(''); const [error, setError] = useState(''); const [loading, setLoading] = useState(false); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(''); setLoading(true); // Validation if (!currentPassword || !newPassword || !confirmPassword) { setError('All fields are required'); setLoading(false); return; } if (newPassword.length < 6) { setError('New password must be at least 6 characters long'); setLoading(false); return; } if (newPassword !== confirmPassword) { setError('New passwords do not match'); setLoading(false); return; } if (currentPassword === newPassword) { setError('New password must be different from current password'); setLoading(false); return; } try { await authService.changePassword(user!._id, currentPassword, newPassword); onSuccess(); onClose(); } catch (error: unknown) { setError( error instanceof Error ? error.message : 'Failed to change password' ); } finally { setLoading(false); } }; // Don't show for OAuth users if (!user?.password) { return (

Password Change Not Available

This account was created using OAuth (Google/GitHub). Password changes are not available for OAuth accounts.

); } return (

Change Password

setCurrentPassword(e.target.value)} className='w-full px-3 py-2 border border-slate-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 dark:bg-slate-700 dark:border-slate-600 dark:text-white' placeholder='Enter your current password' />
setNewPassword(e.target.value)} className='w-full px-3 py-2 border border-slate-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 dark:bg-slate-700 dark:border-slate-600 dark:text-white' placeholder='Enter new password (min 6 characters)' />
setConfirmPassword(e.target.value)} className='w-full px-3 py-2 border border-slate-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 dark:bg-slate-700 dark:border-slate-600 dark:text-white' placeholder='Confirm your new password' />
{error && (
{error}
)}
); }; export default ChangePasswordModal;