'use client'; import IconLockDots from '@/components/icon/icon-lock-dots'; import { useRouter } from 'next/navigation'; import React, { useState } from 'react'; import axios from 'axios'; const ComponentsAuthChangePasswordForm = () => { const router = useRouter(); const [currentPassword, setCurrentPassword] = useState(''); const [newPassword, setNewPassword] = useState(''); const [loading, setLoading] = useState(false); const [error, setError] = useState(''); const [success, setSuccess] = useState(''); const submitForm = async (e: React.FormEvent) => { e.preventDefault(); setError(''); setSuccess(''); setLoading(true); try { const token = localStorage.getItem('token'); if (!token) { setError('You are not logged in.'); setLoading(false); return; } // 👉 Create FormData and append fields const formData = new FormData(); formData.append('currentPassword', currentPassword); formData.append('newPassword', newPassword); // 👉 Axios call with Bearer token const res = await axios.post( `http://localhost:3010/api/auth/change-password`, formData, { headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json", // axios will set correct multipart/form-data boundary automatically }, } ); setSuccess(res.data.message || 'Password updated successfully!'); // Optional: force user to re-login after password change localStorage.removeItem('token'); router.push('/login'); } catch (err: any) { const msg = err.response?.data?.error || err.message; setError(msg); } finally { setLoading(false); } }; return (
{/* Current password */}
setCurrentPassword(e.target.value)} required />
{/* New password */}
setNewPassword(e.target.value)} required />
{error &&

{error}

} {success &&

{success}

}
); }; export default ComponentsAuthChangePasswordForm;