Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | 8x 1536x 1536x 1536x 78x 78x 1536x 1536x 1536x 1536x 209x 1536x 6x 1536x 1536x 6x 6x 6x 6x 1536x 8x | // form
import * as Yup from "yup";
import { yupResolver } from "@hookform/resolvers/yup";
import { useForm, SubmitHandler } from "react-hook-form";
// components
import { useMutation, useQueryClient } from "react-query";
import { showMessage } from "@common/Toast/ShowToast";
import { useGetUser } from "@services/api/onboarding/user";
import { customInstance } from "@services/api";
import { PASSWORD_MIN_CHARS } from "@constants/constants";
import { useEffect } from "react";
import { addPasswordValidationRules } from "@validation/utils";
type IFormInputs = {
current_password: string;
new_password: string;
confirm_password: string;
};
export const useSecurityPassword = () => {
const queryClient = useQueryClient();
const { data: userData } = useGetUser();
const schema = Yup.object().shape({
current_password: Yup.string().min(PASSWORD_MIN_CHARS, "Incorrect password"),
new_password: addPasswordValidationRules(
Yup.string()
.min(PASSWORD_MIN_CHARS, `Password should have ${PASSWORD_MIN_CHARS} characters minimum`)
.test({
name: "validator-current-new-password",
test: function (value, context) {
const currentPassword = context.parent.current_password;
return value !== currentPassword
? true
: this.createError({
message: "Current password and new password should not be same",
path: "new_password",
});
},
})
),
confirm_password: Yup.string()
.required("Password is required")
.oneOf([Yup.ref("new_password")], "Passwords do not match"),
});
const methods = useForm<IFormInputs>({
resolver: yupResolver(schema),
mode: "onChange",
defaultValues,
});
const {
setError,
reset,
formState: { isDirty },
watch,
} = methods;
const values = watch();
useEffect(() => {
Eif (!values.current_password || !values.new_password) return;
(async () => {
await methods.trigger("new_password");
})();
}, [values.current_password]);
const changePwdMutation = useMutation((data: any) => {
return customInstance({
url: `/users/${userData.accID}`,
method: "PATCH",
data,
});
});
const { isLoading } = changePwdMutation;
const onSubmit: SubmitHandler<IFormInputs> = (data) => {
changePwdMutation.mutate(
{
newPassword: data.new_password,
currentPassword: data.current_password,
},
{
onSuccess: () => {
showMessage("Success", "Password reset successfully");
queryClient.invalidateQueries("user");
reset();
},
onError: (error: any) => {
const usedPasswordMessage =
"The new password must not match the previous five.";
if (error.response?.data?.message === usedPasswordMessage) {
return setError("new_password", {
message: usedPasswordMessage,
});
} else {
setError("current_password", {
message:
"The provided password does not match the user's current password",
});
}
},
},
);
};
return { methods, onSubmit, isLoading, userData, isDirty };
};
const defaultValues = {
current_password: "",
new_password: "",
confirm_password: "",
};
|