diff --git a/ipi-survey-platform/src/pages/ChangePassword/ChangePassword.jsx b/ipi-survey-platform/src/pages/ChangePassword/ChangePassword.jsx index 0224608..4d2d294 100644 --- a/ipi-survey-platform/src/pages/ChangePassword/ChangePassword.jsx +++ b/ipi-survey-platform/src/pages/ChangePassword/ChangePassword.jsx @@ -1,208 +1,192 @@ -import React from 'react'; -import Logo from '../../assets/images/FCSCLogo.svg'; -import { useNavigate } from 'react-router-dom'; -import { changePassword } from '../../services/auth/authService.js'; +import React from "react"; +import Logo from "../../assets/images/FCSCLogo.svg"; +import { useNavigate, useLocation } from "react-router-dom"; +import apiClient from "../../services/api/apiClient.js"; const ChangePassword = () => { const navigate = useNavigate(); - const [currentPassword, setCurrentPassword] = React.useState(''); - const [newPassword, setNewPassword] = React.useState(''); - const [confirmPassword, setConfirmPassword] = React.useState(''); - const [showCurrentPassword, setShowCurrentPassword] = React.useState(false); + const location = useLocation(); + + const registeredEmail = location.state?.email || ""; + + const [otp, setOtp] = React.useState(""); + const [newPassword, setNewPassword] = React.useState(""); + const [confirmPassword, setConfirmPassword] = React.useState(""); + const [showOtp, setShowOtp] = React.useState(false); const [showNewPassword, setShowNewPassword] = React.useState(false); const [showConfirmPassword, setShowConfirmPassword] = React.useState(false); const [loading, setLoading] = React.useState(false); - const [error, setError] = React.useState(''); - const [success, setSuccess] = React.useState(''); + const [error, setError] = React.useState(""); + const [success, setSuccess] = React.useState(""); - const handleSubmit = async (event) => { - event.preventDefault(); - setError(''); - setSuccess(''); + const handleSubmit = async (e) => { + e.preventDefault(); + setError(""); + setSuccess(""); - if (!currentPassword || !newPassword) { - setError('Please fill in all required fields.'); + if (!registeredEmail) { + setError("Missing registered email. Please restart the process."); + return; + } + + if (!otp || !newPassword || !confirmPassword) { + setError("Please fill in all fields."); return; } if (newPassword !== confirmPassword) { - setError('New password and confirmation do not match.'); + setError("Passwords do not match."); return; } setLoading(true); try { - const response = await changePassword({ currentPassword, newPassword }); - const status = response?.status ?? 'success'; - const message = response?.message || 'Password updated successfully. Please sign in again with your new password.'; - if (status !== 'success') { - throw new Error(message); + const payload = { + registered_email: registeredEmail, + otp: otp.trim(), + password: newPassword, + confirm_password: confirmPassword, + }; + + const response = await apiClient.post("/forgot-password/verify-otp", payload); + + if (response?.data) { + setSuccess("Password changed successfully. Redirecting to login..."); + setTimeout(() => navigate("/login"), 1500); + } else { + setError("Something went wrong. Please try again."); } - try { - sessionStorage.removeItem('auth_token'); - sessionStorage.removeItem('user_profile'); - sessionStorage.removeItem('user_role'); - sessionStorage.removeItem('establishment_id'); - } catch (storageError) { - console.warn('Unable to clear stored session', storageError); - } - setSuccess(message); - setTimeout(() => { - navigate('/login'); - }, 1500); } catch (err) { - const message = err?.response?.data?.message || err?.message || 'Unable to change password.'; - setError(message); + const msg = + err.response?.data?.message || + err.message || + "Password change failed. Please verify the details."; + setError(msg); } finally { setLoading(false); } }; + const ToggleableInput = ({ label, type = "password", value, onChange, show, toggleShow, placeholder }) => ( +
Keep your account secure by updating your password.
++ Enter the verification code sent to your registered email and set a new password. +