'use client' import { useState } from 'react' export default function ChangePasswordForm() { const [current, setCurrent] = useState('') const [newPass, setNewPass] = useState('') const [confirm, setConfirm] = useState('') const [loading, setLoading] = useState(false) const [error, setError] = useState(null) const [success, setSuccess] = useState(false) const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setError(null) setSuccess(false) // Client-side validation if (!current || !newPass || !confirm) { setError('All fields are required.') return } if (newPass.length < 8) { setError('Password must be at least 8 characters.') return } if (newPass !== confirm) { setError('Passwords do not match.') return } if (newPass === current) { setError('New password must differ from current password.') return } setLoading(true) const res = await fetch('/api/auth/change-password', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ currentPassword: current, newPassword: newPass }), }) setLoading(false) if (!res.ok) { const data = await res.json() setError(data.error ?? 'Password change failed.') return } setCurrent('') setNewPass('') setConfirm('') setSuccess(true) } return (

Change Password

setCurrent(e.target.value)} disabled={loading} autoComplete="current-password" className="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:ring-2 focus:ring-blue-500 focus:border-transparent disabled:opacity-50" />
setNewPass(e.target.value)} disabled={loading} autoComplete="new-password" className="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:ring-2 focus:ring-blue-500 focus:border-transparent disabled:opacity-50" />
setConfirm(e.target.value)} disabled={loading} autoComplete="new-password" className="w-full border border-gray-300 rounded-lg px-3 py-2 text-sm focus:ring-2 focus:ring-blue-500 focus:border-transparent disabled:opacity-50" />
{error &&

{error}

} {success &&

Password changed successfully.

}
) }