diff --git a/.chores.md b/.chores.md index 6d45e9a11..9786ce569 100644 --- a/.chores.md +++ b/.chores.md @@ -4,7 +4,7 @@ - [ ] Validation on submit - [ ] Take validation out from the component. - [x] SetNewPassword - - [ ] Register + - [x] Register - [ ] Refactor on change to deal with step three specificity - [ ] Fix Input enter not working - [ ] PasswordPanel @@ -27,3 +27,8 @@ - [x] Confirm Password Left Blank - [x] Password Change After Successful Match - [x] Avoid submission in new password if password or confirm empty, and on error +- [ ] Update validation for password on backend + +//TODO Apply custom hook to SetNewPassword +// Enter on input triggers back action on steps one and two +// When user registers, we see sign up page for half a second diff --git a/Client/src/Pages/Auth/Login.jsx b/Client/src/Pages/Auth/Login.jsx index c0975f2d0..687abb7e6 100644 --- a/Client/src/Pages/Auth/Login.jsx +++ b/Client/src/Pages/Auth/Login.jsx @@ -130,17 +130,14 @@ const StepOne = ({ form, errors, onSubmit, onChange, onBack }) => { return ( <> Log In Enter your email address - +
{ const navigate = useNavigate(); const inputRef = useRef(null); + console.log({ errors }); + + const errorsMap = { + length: "Must be at least 8 characters long", + uppercase: "Must contain at least one upper character", + lowercase: "Must contain at least one lower character", + number: "Must contain at least one number", + special: "Must contain at least one special character", + }; + useEffect(() => { if (inputRef.current) { inputRef.current.focus(); @@ -235,7 +242,7 @@ const StepTwo = ({ form, errors, onSubmit, onChange, onBack }) => { return ( <> @@ -244,64 +251,66 @@ const StepTwo = ({ form, errors, onSubmit, onChange, onBack }) => { Enter your password - + - - + + + - - - - { if (action.payload.success) { const authToken = action.payload.data; localStorage.setItem("token", authToken); - /* navigate("/"); */ navigate("/monitors"); createToast({ body: "Welcome! Your account was created successfully.", diff --git a/Client/src/Pages/Auth/Register/StepThree/index.jsx b/Client/src/Pages/Auth/Register/StepThree/index.jsx index 0f1e540f7..b225f09e8 100644 --- a/Client/src/Pages/Auth/Register/StepThree/index.jsx +++ b/Client/src/Pages/Auth/Register/StepThree/index.jsx @@ -1,11 +1,10 @@ -import { useEffect, useRef /* , useState */ } from "react"; +import { useEffect, useRef } from "react"; import PropTypes from "prop-types"; import { useTheme } from "@emotion/react"; import { Box, Button, Stack, Typography } from "@mui/material"; import ArrowBackRoundedIcon from "@mui/icons-material/ArrowBackRounded"; import Field from "../../../../Components/Inputs/Field"; import Check from "../../../../Components/Check/Check"; -/* import { credentials } from "../../../../Validation/validation"; */ import { useValidatePassword } from "../../hooks/useValidatePassword"; StepThree.propTypes = { diff --git a/Client/src/Pages/Auth/SetNewPassword.jsx b/Client/src/Pages/Auth/SetNewPassword.jsx index 5f0acef15..92d6973f1 100644 --- a/Client/src/Pages/Auth/SetNewPassword.jsx +++ b/Client/src/Pages/Auth/SetNewPassword.jsx @@ -1,4 +1,4 @@ -import { useId, useState } from "react"; +import { useId } from "react"; import { useDispatch, useSelector } from "react-redux"; import { useNavigate } from "react-router-dom"; import { useParams } from "react-router-dom"; @@ -26,11 +26,6 @@ const SetNewPassword = () => { const confirmPasswordId = useId(); const { form, errors, handleChange, feedbacks } = useValidatePassword(); - // const [errors, setErrors] = useState({}); - // const [form, setForm] = useState({ - // password: "", - // confirm: "", - // }); const { isLoading } = useSelector((state) => state.auth); const { token } = useParams(); @@ -65,60 +60,6 @@ const SetNewPassword = () => { } }; - // const handleChange = (event) => { - // const { value, name } = event.target; - // setForm((prev) => ({ ...prev, [name]: value })); - - // const validateValue = { [name]: value }; - // const validateOptions = { abortEarly: false, context: { password: form.password } }; - // if (name === "password" && form.confirm.length > 0) { - // validateValue.confirm = form.confirm; - // validateOptions.context = { password: value }; - // } else if (name === "confirm") { - // validateValue.password = form.password; - // } - - // const { error } = credentials.validate(validateValue, validateOptions); - // const errors = error?.details.map((error) => ({ - // path: error.path[0], - // message: error.message, - // })); - // const errorsByPath = - // errors && - // errors.reduce((acc, { path, message }) => { - // if (!acc[path]) { - // acc[path] = []; - // } - // acc[path].push(message); - // return acc; - // }, {}); - // setErrors(() => (errorsByPath ? { ...errorsByPath } : {})); - // }; - - // 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; - - // if (isCorrect) { - // return "success"; - // } else if (hasError) { - // return "error"; - // } else { - // return "info"; - // } - // }; - - // const feedbacks = { - // length: getFeedbackStatus("password", "length"), - // special: getFeedbackStatus("password", "special"), - // number: getFeedbackStatus("password", "number"), - // uppercase: getFeedbackStatus("password", "uppercase"), - // lowercase: getFeedbackStatus("password", "lowercase"), - // confirm: getFeedbackStatus("confirm", "different"), - // }; - return ( (value, helpers) => { // Auth //**************************************** +const passwordPattern = + /^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!?@#$%^&*()\-_=+[\]{};:'",.<>~`|\\/])[A-Za-z0-9!?@#$%^&*()\-_=+[\]{};:'",.<>~`|\\/]+$/; + const loginValidation = joi.object({ email: joi .string() @@ -30,13 +33,7 @@ const loginValidation = joi.object({ } return lowercasedValue; }), - password: joi - .string() - .min(8) - .required() - .pattern( - /^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*()])[A-Za-z0-9!@#$%^&*()]+$/ - ), + password: joi.string().min(8).required().pattern(passwordPattern), }); const registrationBodyValidation = joi.object({ @@ -59,13 +56,7 @@ const registrationBodyValidation = joi.object({ } return lowercasedValue; }), - password: joi - .string() - .min(8) - .required() - .pattern( - /^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*()])[A-Za-z0-9!@#$%^&*()]+$/ - ), + password: joi.string().min(8).required().pattern(passwordPattern), profileImage: joi.any(), role: joi .array() @@ -84,18 +75,8 @@ const editUserBodyValidation = joi.object({ firstName: joi.string().pattern(/^[A-Za-z]+$/), lastName: joi.string().pattern(/^[A-Za-z]+$/), profileImage: joi.any(), - newPassword: joi - .string() - .min(8) - .pattern( - /^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*()])[A-Za-z0-9!@#$%^&*()]+$/ - ), - password: joi - .string() - .min(8) - .pattern( - /^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*()])[A-Za-z0-9!@#$%^&*()]+$/ - ), + newPassword: joi.string().min(8).pattern(passwordPattern), + password: joi.string().min(8).pattern(passwordPattern), deleteProfileImage: joi.boolean(), role: joi.array(), }); @@ -113,13 +94,7 @@ const recoveryTokenValidation = joi.object({ const newPasswordValidation = joi.object({ recoveryToken: joi.string().required(), - password: joi - .string() - .min(8) - .required() - .pattern( - /^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!?@#$%^&*()\-_=+[\]{};:'",.<>~`|\\/])[A-Za-z0-9!?@#$%^&*()\-_=+[\]{};:'",.<>~`|\\/]+$/ - ), + password: joi.string().min(8).required().pattern(passwordPattern), confirm: joi.string(), });