diff --git a/src/pages/authentication/ChangePassword.jsx b/src/pages/authentication/ChangePassword.jsx index f149a30e..9aff890b 100644 --- a/src/pages/authentication/ChangePassword.jsx +++ b/src/pages/authentication/ChangePassword.jsx @@ -5,37 +5,53 @@ import { zodResolver } from "@hookform/resolvers/zod"; import showToast from "../../services/toastService"; import AuthRepository from "../../repositories/AuthRepository"; import "./page-auth.css"; +import { useSelector } from "react-redux"; -const ChangePasswordSchema = z.object({ - email: z.string().email(), - newPassword: z - .string() - .min(8, "Password must be at least 8 characters") - .regex(/[A-Z]/, "Must contain an uppercase letter") - .regex(/[a-z]/, "Must contain a lowercase letter") - .regex(/\d/, "Must contain a number") - .regex(/[!@#$%^&*()_+{}\[\]:;<>,.?~\\/-]/, "Must contain a special character"), - oldPassword: z.string().min(1, "Old Password is required"), -}); +// --- Zod Schema for validation --- +const ChangePasswordSchema = z + .object({ + newPassword: z + .string() + .min(8, "Password must be at least 8 characters") + .regex(/[A-Z]/, "Must contain an uppercase letter") + .regex(/[a-z]/, "Must contain a lowercase letter") + .regex(/\d/, "Must contain a number") + .regex(/[!@#$%^&*()_+{}\[\]:;<>,.?~\\/-]/, "Must contain a special character"), + confirmPassword: z.string().min(1, "Confirm Password is required"), + oldPassword: z.string().min(1, "Old Password is required"), + }) + .refine((data) => data.newPassword === data.confirmPassword, { + message: "New password and Confirm password do not match", + path: ["confirmPassword"], + }); const ChangePasswordPage = ({ onClose }) => { const [loading, setLoading] = useState(false); - const [hidepass, setHidepass] = useState(true); - const [hidepass1, setHidepass1] = useState(true); + const [hideOldPass, setHideOldPass] = useState(true); + const [hideNewPass, setHideNewPass] = useState(true); + const [hideConfirmPass, setHideConfirmPass] = useState(true); + const loggedUser = useSelector((store) => store.globalVariables.loginUser); const { register, handleSubmit, formState: { errors }, reset, + setError, } = useForm({ resolver: zodResolver(ChangePasswordSchema), }); const onChangePassword = async (data) => { try { + let formData = { + email: loggedUser?.employeeInfo?.email, + oldPassword: data.oldPassword, + newPassword: data.newPassword, + }; + console.log(formData); setLoading(true); - await AuthRepository.changepassword(data); + await AuthRepository.changepassword(formData); showToast("Your Password changed Successfully", "success"); setLoading(false); reset(); @@ -57,7 +73,7 @@ const ChangePasswordPage = ({ onClose }) => { className="modal-dialog" role="document" style={{ - maxWidth: "400px", + maxWidth: "600px", width: "100%", }} > @@ -73,159 +89,125 @@ const ChangePasswordPage = ({ onClose }) => {
Change Password

- Enter your email and old password to update. + Enter old and new password to update.

+ {/* Old Password */}
- - - {errors.email && ( -
- {errors.email.message} -
- )} -
- - {/*
-
+
{errors.oldPassword && ( -

{errors.oldPassword.message}

+

+ {errors.oldPassword.message} +

)}
-
- -
- - -
- {errors.newPassword && ( -
- {errors.newPassword.message} +
+
+ +
+ +
- )} -
*/} - -
- -
- - + {errors.newPassword && ( +
+ {errors.newPassword.message} +
+ )}
- {errors.oldPassword && ( -

{errors.oldPassword.message}

- )} -
-
- -
- - -
- {errors.newPassword && ( -
- {errors.newPassword.message} + {/* Confirm Password */} +
+ +
+ +
- )} + {errors.confirmPassword && ( +
+ {errors.confirmPassword.message} +
+ )} +
- -
-

- Password must be at least 8 characters -

-

- Password must contain at least one uppercase letter -

-

- Password must contain at least one number -

-

- Password must contain at least one special character -

-
- -
+ {/* Action Buttons */} +
+ - +
+
+

+ Password must : +

+

+ - be at least 8 characters +

+

+ - must contain at least one uppercase letter +

+

+ - must contain at least one lowercase letter +

+

+ - must contain at least one number +

+

+ - must contain at least one special character +