diff --git a/Client/src/Pages/Auth/SetNewPassword.jsx b/Client/src/Pages/Auth/SetNewPassword.jsx index 9d1497b47..9edd13d97 100644 --- a/Client/src/Pages/Auth/SetNewPassword.jsx +++ b/Client/src/Pages/Auth/SetNewPassword.jsx @@ -1,4 +1,4 @@ -import { useState } from "react"; +import { useId, useState } from "react"; import { useDispatch, useSelector } from "react-redux"; import { useNavigate } from "react-router-dom"; import { useParams } from "react-router-dom"; @@ -21,6 +21,9 @@ const SetNewPassword = () => { const dispatch = useDispatch(); const theme = useTheme(); + const passwordId = useId(); + const confirmPasswordId = useId(); + const [errors, setErrors] = useState({}); const [form, setForm] = useState({ password: "", @@ -36,11 +39,10 @@ const SetNewPassword = () => { const passwordForm = { ...form }; const { error } = credentials.validate(passwordForm, { abortEarly: false, - /* context: { password: form.password }, */ + context: { password: form.password }, }); if (error) { - // validation errors const newErrors = {}; error.details.forEach((err) => { newErrors[err.path[0]] = err.message; @@ -77,46 +79,43 @@ const SetNewPassword = () => { }; const handleChange = (event) => { - //TODO Change from id to name const { value, name } = event.target; - setForm((prev) => ({ - ...prev, - [name]: value, - })); + setForm((prev) => ({ ...prev, [name]: value })); - const { error } = credentials.validate( - { [name]: value }, - { - abortEarly: false, - context: { password: form.password }, - } - ); - - const errors = error.details.map((error) => error.message); + const validateValue = { [name]: value }; + const validateOptions = { abortEarly: false, context: { password: form.password } }; + const { error } = credentials.validate(validateValue, validateOptions); + const errors = error?.details.map((error) => error.message); setErrors((prev) => ({ ...prev, [name]: errors })); }; - console.log(errors); + const getFeedbackStatus = (field, criteria) => { + const fieldErrors = errors[field]; + const isFieldEmpty = form[field].length === 0; + const hasError = fieldErrors?.includes(criteria) || fieldErrors?.includes("empty"); + const isCorrect = !isFieldEmpty && !hasError; - /* function getCheckStatus() */ + if (isCorrect) { + return "success"; + } else if (hasError) { + return "error"; + } else { + return "info"; + } + }; const feedbacks = { - length: "info" /* !errors.password - ? "info" - : errors.password === "length" || errors.password === "empty" - ? "error" - : "success", */, - special: "info" /* !errors - ? "info" - : errors.password === "length" || errors.password === "empty" - ? "error" - : "success", */, - number: "info", - upper: "success", - lower: "error", + length: getFeedbackStatus("password", "length"), + special: getFeedbackStatus("password", "special"), + number: getFeedbackStatus("password", "number"), + uppercase: getFeedbackStatus("password", "uppercase"), + lowercase: getFeedbackStatus("password", "lowercase"), + confirm: getFeedbackStatus("confirm", "different"), }; + console.log({ feedbacks }); + return ( { onSubmit={handleSubmit} > { placeholder="••••••••" value={form.password} onChange={handleChange} - error={errors.password} + /* error={errors.password} */ /> { onSubmit={handleSubmit} > { placeholder="••••••••" value={form.confirm} onChange={handleChange} - error={errors.confirm} + /* error={errors.confirm} */ /> { upper character } - variant={feedbacks.upper} + variant={feedbacks.uppercase} /> { lower character } - variant={feedbacks.lower} + variant={feedbacks.lowercase} + /> + + + Confirm password and password + {" "} + must match + + } + variant={feedbacks.confirm} /> diff --git a/Client/src/Validation/validation.js b/Client/src/Validation/validation.js index 82dee8541..79533ee1b 100644 --- a/Client/src/Validation/validation.js +++ b/Client/src/Validation/validation.js @@ -39,7 +39,7 @@ const passwordSchema = joi return value; }) .custom((value, helpers) => { - if (!/[!@#$%^&*]/.test(value)) { + if (!/[!?@#$%^&*()\-_=+[\]{};:'",.<>~`|\\/]/.test(value)) { return helpers.message("special"); } return value;