fix: backend validation password
This commit is contained in:
1 parent
686217944d
commit
fd38a9d7eb
5 files changed
+127
-116
No files matched your search
@@ -213,44 +213,42 @@ const Register = ({ isSuperAdmin }) => {
|
|||||||
const handleStepThree = async (e) => {
|
const handleStepThree = async (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
const { password, confirm } = e.target.elements;
|
const { password, confirm } = e.target.elements;
|
||||||
console.log(password.value);
|
let registerForm = {
|
||||||
console.log(confirm.value);
|
...form,
|
||||||
// let registerForm = {
|
password: password.value,
|
||||||
// ...form,
|
confirm: confirm.value,
|
||||||
|
role: isSuperAdmin ? ["superadmin"] : form.role,
|
||||||
|
inviteToken: token ? token : "", // Add the token to the request for verification
|
||||||
|
};
|
||||||
|
let error = validateForm(registerForm, {
|
||||||
|
context: { password: registerForm.password },
|
||||||
|
});
|
||||||
|
if (error) {
|
||||||
|
handleError(error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// role: isSuperAdmin ? ["superadmin"] : form.role,
|
delete registerForm.confirm;
|
||||||
// inviteToken: token ? token : "", // Add the token to the request for verification
|
const action = await dispatch(register(registerForm));
|
||||||
// };
|
if (action.payload.success) {
|
||||||
// let error = validateForm(registerForm, {
|
const authToken = action.payload.data;
|
||||||
// context: { password: form.password },
|
localStorage.setItem("token", authToken);
|
||||||
// });
|
/* navigate("/"); */
|
||||||
// if (error) {
|
navigate("/monitors");
|
||||||
// handleError(error);
|
createToast({
|
||||||
// return;
|
body: "Welcome! Your account was created successfully.",
|
||||||
// }
|
});
|
||||||
|
} else {
|
||||||
// delete registerForm.confirm;
|
if (action.payload) {
|
||||||
// const action = await dispatch(register(registerForm));
|
createToast({
|
||||||
// if (action.payload.success) {
|
body: action.payload.msg,
|
||||||
// const authToken = action.payload.data;
|
});
|
||||||
// localStorage.setItem("token", authToken);
|
} else {
|
||||||
// navigate("/");
|
createToast({
|
||||||
// createToast({
|
body: "Unknown error.",
|
||||||
// body: "Welcome! Your account was created successfully.",
|
});
|
||||||
// });
|
}
|
||||||
// } else {
|
}
|
||||||
// if (action.payload) {
|
|
||||||
// // dispatch errors
|
|
||||||
// createToast({
|
|
||||||
// body: action.payload.msg,
|
|
||||||
// });
|
|
||||||
// } else {
|
|
||||||
// // unknown errors
|
|
||||||
// createToast({
|
|
||||||
// body: "Unknown error.",
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleChange = (event) => {
|
const handleChange = (event) => {
|
||||||
|
|||||||
@@ -136,6 +136,10 @@ function StepThree({ onSubmit, onBack }) {
|
|||||||
"& svg.MuiSvgIcon-root": {
|
"& svg.MuiSvgIcon-root": {
|
||||||
mr: theme.spacing(3),
|
mr: theme.spacing(3),
|
||||||
},
|
},
|
||||||
|
":focus-visible": {
|
||||||
|
outline: `2px solid ${theme.palette.primary.dark}`,
|
||||||
|
outlineOffset: "4px",
|
||||||
|
},
|
||||||
}}
|
}}
|
||||||
props={{ tabIndex: -1 }}
|
props={{ tabIndex: -1 }}
|
||||||
>
|
>
|
||||||
@@ -147,7 +151,11 @@ function StepThree({ onSubmit, onBack }) {
|
|||||||
variant="contained"
|
variant="contained"
|
||||||
color="primary"
|
color="primary"
|
||||||
/* onClick={onSubmit} */
|
/* onClick={onSubmit} */
|
||||||
disabled={errors.email && true}
|
disabled={
|
||||||
|
form.password.length === 0 ||
|
||||||
|
form.confirm.length === 0 ||
|
||||||
|
Object.keys(errors).length !== 0
|
||||||
|
}
|
||||||
sx={{ width: "30%" }}
|
sx={{ width: "30%" }}
|
||||||
>
|
>
|
||||||
Continue
|
Continue
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import LockIcon from "../../assets/icons/lock.svg?react";
|
|||||||
import Logo from "../../assets/icons/bwu-icon.svg?react";
|
import Logo from "../../assets/icons/bwu-icon.svg?react";
|
||||||
import Background from "../../assets/Images/background-grid.svg?react";
|
import Background from "../../assets/Images/background-grid.svg?react";
|
||||||
import "./index.css";
|
import "./index.css";
|
||||||
|
import { useValidatePassword } from "./hooks/useValidatePassword";
|
||||||
|
|
||||||
const SetNewPassword = () => {
|
const SetNewPassword = () => {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
@@ -24,11 +25,12 @@ const SetNewPassword = () => {
|
|||||||
const passwordId = useId();
|
const passwordId = useId();
|
||||||
const confirmPasswordId = useId();
|
const confirmPasswordId = useId();
|
||||||
|
|
||||||
const [errors, setErrors] = useState({});
|
const { form, errors, handleChange, feedbacks } = useValidatePassword();
|
||||||
const [form, setForm] = useState({
|
// const [errors, setErrors] = useState({});
|
||||||
password: "",
|
// const [form, setForm] = useState({
|
||||||
confirm: "",
|
// password: "",
|
||||||
});
|
// confirm: "",
|
||||||
|
// });
|
||||||
|
|
||||||
const { isLoading } = useSelector((state) => state.auth);
|
const { isLoading } = useSelector((state) => state.auth);
|
||||||
const { token } = useParams();
|
const { token } = useParams();
|
||||||
@@ -63,59 +65,59 @@ const SetNewPassword = () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleChange = (event) => {
|
// const handleChange = (event) => {
|
||||||
const { value, name } = event.target;
|
// const { value, name } = event.target;
|
||||||
setForm((prev) => ({ ...prev, [name]: value }));
|
// setForm((prev) => ({ ...prev, [name]: value }));
|
||||||
|
|
||||||
const validateValue = { [name]: value };
|
// const validateValue = { [name]: value };
|
||||||
const validateOptions = { abortEarly: false, context: { password: form.password } };
|
// const validateOptions = { abortEarly: false, context: { password: form.password } };
|
||||||
if (name === "password" && form.confirm.length > 0) {
|
// if (name === "password" && form.confirm.length > 0) {
|
||||||
validateValue.confirm = form.confirm;
|
// validateValue.confirm = form.confirm;
|
||||||
validateOptions.context = { password: value };
|
// validateOptions.context = { password: value };
|
||||||
} else if (name === "confirm") {
|
// } else if (name === "confirm") {
|
||||||
validateValue.password = form.password;
|
// validateValue.password = form.password;
|
||||||
}
|
// }
|
||||||
|
|
||||||
const { error } = credentials.validate(validateValue, validateOptions);
|
// const { error } = credentials.validate(validateValue, validateOptions);
|
||||||
const errors = error?.details.map((error) => ({
|
// const errors = error?.details.map((error) => ({
|
||||||
path: error.path[0],
|
// path: error.path[0],
|
||||||
message: error.message,
|
// message: error.message,
|
||||||
}));
|
// }));
|
||||||
const errorsByPath =
|
// const errorsByPath =
|
||||||
errors &&
|
// errors &&
|
||||||
errors.reduce((acc, { path, message }) => {
|
// errors.reduce((acc, { path, message }) => {
|
||||||
if (!acc[path]) {
|
// if (!acc[path]) {
|
||||||
acc[path] = [];
|
// acc[path] = [];
|
||||||
}
|
// }
|
||||||
acc[path].push(message);
|
// acc[path].push(message);
|
||||||
return acc;
|
// return acc;
|
||||||
}, {});
|
// }, {});
|
||||||
setErrors(() => (errorsByPath ? { ...errorsByPath } : {}));
|
// setErrors(() => (errorsByPath ? { ...errorsByPath } : {}));
|
||||||
};
|
// };
|
||||||
|
|
||||||
const getFeedbackStatus = (field, criteria) => {
|
// const getFeedbackStatus = (field, criteria) => {
|
||||||
const fieldErrors = errors[field];
|
// const fieldErrors = errors[field];
|
||||||
const isFieldEmpty = form[field].length === 0;
|
// const isFieldEmpty = form[field].length === 0;
|
||||||
const hasError = fieldErrors?.includes(criteria) || fieldErrors?.includes("empty");
|
// const hasError = fieldErrors?.includes(criteria) || fieldErrors?.includes("empty");
|
||||||
const isCorrect = !isFieldEmpty && !hasError;
|
// const isCorrect = !isFieldEmpty && !hasError;
|
||||||
|
|
||||||
if (isCorrect) {
|
// if (isCorrect) {
|
||||||
return "success";
|
// return "success";
|
||||||
} else if (hasError) {
|
// } else if (hasError) {
|
||||||
return "error";
|
// return "error";
|
||||||
} else {
|
// } else {
|
||||||
return "info";
|
// return "info";
|
||||||
}
|
// }
|
||||||
};
|
// };
|
||||||
|
|
||||||
const feedbacks = {
|
// const feedbacks = {
|
||||||
length: getFeedbackStatus("password", "length"),
|
// length: getFeedbackStatus("password", "length"),
|
||||||
special: getFeedbackStatus("password", "special"),
|
// special: getFeedbackStatus("password", "special"),
|
||||||
number: getFeedbackStatus("password", "number"),
|
// number: getFeedbackStatus("password", "number"),
|
||||||
uppercase: getFeedbackStatus("password", "uppercase"),
|
// uppercase: getFeedbackStatus("password", "uppercase"),
|
||||||
lowercase: getFeedbackStatus("password", "lowercase"),
|
// lowercase: getFeedbackStatus("password", "lowercase"),
|
||||||
confirm: getFeedbackStatus("confirm", "different"),
|
// confirm: getFeedbackStatus("confirm", "different"),
|
||||||
};
|
// };
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Stack
|
<Stack
|
||||||
|
|||||||
@@ -1,6 +1,21 @@
|
|||||||
import { useState } from "react";
|
import { useMemo, useState } from "react";
|
||||||
import { credentials } from "../../../Validation/validation";
|
import { credentials } from "../../../Validation/validation";
|
||||||
|
|
||||||
|
const getFeedbackStatus = (form, errors, 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";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
function useValidatePassword() {
|
function useValidatePassword() {
|
||||||
const [errors, setErrors] = useState({});
|
const [errors, setErrors] = useState({});
|
||||||
const [form, setForm] = useState({
|
const [form, setForm] = useState({
|
||||||
@@ -38,29 +53,17 @@ function useValidatePassword() {
|
|||||||
setErrors(() => (errorsByPath ? { ...errorsByPath } : {}));
|
setErrors(() => (errorsByPath ? { ...errorsByPath } : {}));
|
||||||
};
|
};
|
||||||
|
|
||||||
const getFeedbackStatus = (field, criteria) => {
|
const feedbacks = useMemo(
|
||||||
const fieldErrors = errors[field];
|
() => ({
|
||||||
const isFieldEmpty = form[field].length === 0;
|
length: getFeedbackStatus(form, errors, "password", "length"),
|
||||||
const hasError = fieldErrors?.includes(criteria) || fieldErrors?.includes("empty");
|
special: getFeedbackStatus(form, errors, "password", "special"),
|
||||||
const isCorrect = !isFieldEmpty && !hasError;
|
number: getFeedbackStatus(form, errors, "password", "number"),
|
||||||
|
uppercase: getFeedbackStatus(form, errors, "password", "uppercase"),
|
||||||
if (isCorrect) {
|
lowercase: getFeedbackStatus(form, errors, "password", "lowercase"),
|
||||||
return "success";
|
confirm: getFeedbackStatus(form, errors, "confirm", "different"),
|
||||||
} else if (hasError) {
|
}),
|
||||||
return "error";
|
[form, errors]
|
||||||
} 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 { handleChange, feedbacks, form, errors };
|
return { handleChange, feedbacks, form, errors };
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -118,7 +118,7 @@ const newPasswordValidation = joi.object({
|
|||||||
.min(8)
|
.min(8)
|
||||||
.required()
|
.required()
|
||||||
.pattern(
|
.pattern(
|
||||||
/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*()])[A-Za-z0-9!@#$%^&*()]+$/
|
/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!?@#$%^&*()\-_=+[\]{};:'",.<>~`|\\/])[A-Za-z0-9!?@#$%^&*()\-_=+[\]{};:'",.<>~`|\\/]+$/
|
||||||
),
|
),
|
||||||
confirm: joi.string(),
|
confirm: joi.string(),
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in new issue
Block a user