diff --git a/src/components/AuthPages/VerifyPassword/index2.jsx b/src/components/AuthPages/VerifyPassword/index2.jsx new file mode 100644 index 0000000..f43c319 --- /dev/null +++ b/src/components/AuthPages/VerifyPassword/index2.jsx @@ -0,0 +1,269 @@ +import { useEffect, useState } from "react"; +import { Link, useLocation, useNavigate } from "react-router-dom"; +import WrenchBoard from "../../../assets/images/wrenchboard-logo-text.png"; +import usersService from "../../../services/UsersService"; +import InputCom from "../../Helpers/Inputs/InputCom"; +import AuthLayout from "../AuthLayout"; +import ForgetPwdResponse from "../ForgetPwdResponse"; +import PasswordValidator from "../../../lib/PasswordValidator"; +import LoadingSpinner from "../../Spinners/LoadingSpinner"; + +const VerifyPassword = () => { + const [password, setPassword] = useState(""); + const [confirmPassword, setConfirmPassword] = useState(""); + const [requestStatus, setRequestStatus] = useState({loading: true, status:false, data: []}) + const [msgError, setMsgError] = useState(""); + const [linkLoader, setLinkLoader] = useState(false); + const [linkSuccess, setLinkSuccess] = useState(null); + const [showPassword, setShowPassword] = useState(false); + const navigate = useNavigate(); + const location = useLocation(); + const queryParams = new URLSearchParams(location?.search); + const token = queryParams.get("passlink"); + const userApi = new usersService(); + + // To Show and Hide Password + const togglePasswordVisibility = () => { + setShowPassword(!showPassword); + }; + + // Password + const handlePassword = (e) => { + let { name, value } = e?.target; + if (name == "password") setPassword(value); + if (name == "confirm_password") setConfirmPassword(value); + }; + + const completeReset = async () => { + if(!password || !confirmPassword){ // CHECKS IF PASSWORD IS EMPTY + setMsgError("Please fill in fields"); + return setTimeout(() => { + setMsgError(null); + }, process.env.REACT_APP_RESET_START_ERROR_TIMEOUT); + } + if(password != confirmPassword){ // CHECKS IF PASSWORD EQUALS CONFIRM PASSWORD + setMsgError("Passwords does not match"); + return setTimeout(() => { + setMsgError(null); + }, process.env.REACT_APP_RESET_START_ERROR_TIMEOUT); + } + if(password.length < 6){ // CHECKS IF PASSWORD LENGTH IS UPTO 6 CHARACTERS + setMsgError("Password must be upto six characters"); + return setTimeout(() => { + setMsgError(null); + }, process.env.REACT_APP_RESET_START_ERROR_TIMEOUT); + } + if(!PasswordValidator(password)){ // CHECKS IF PASSWORD IS VALID + setMsgError("Password must contain alphanumeric, uppercase and special character: eg: Password1@"); + return setTimeout(() => { + setMsgError(null); + }, process.env.REACT_APP_RESET_START_ERROR_TIMEOUT); + } + try { + setLinkLoader(true); + var reqData = { + sessionid: "DUMMY-CANNOT_BE_EMPTY", + reset_link: token, + newpass: password, + m_uid: requestStatus.data?.m_uid || '', + reset_uid: requestStatus.data?.reset_uid || '', + step: 300, + action: 730, + }; + const res = await userApi?.CompleteResetPassword(reqData); + + if (res.status == 200) { + const { data } = res; + if (data?.internal_return >= 0) { + // setTimeout(() => { + // navigate("/login", { replace: true }); + // setLinkLoader(false); + // }, 2000); + setLinkSuccess(true); + } else { + setLinkLoader(false); + setMsgError("An error occurred"); + setLinkSuccess(false); + } + } else { + setLinkLoader(false); + setLinkSuccess(false); + } + } catch (error) { + setLinkLoader(false); + setLinkSuccess(false); + throw new Error(error); + } finally { + setTimeout(() => { + setMsgError(null); + }, process.env.REACT_APP_SIGNUP_ERROR_TIMEOUT); + } + }; + + const verifyResetPwd = () => { // FUNCTION TO VERIFY RESET PASSWORD LINK + setRequestStatus({loading: true, status:false, data: []}) + var reqData = { + sessionid: "DUMMY-CANNOT_BE_EMPTY", + reset_link: token, + step: 200, + action: 730, + }; + userApi.CompleteResetPassword(reqData).then(res => { + if(res.status != 200 || res.data.internal_return < 0){ + return setRequestStatus({loading: false, status:false, data: []}) + } + setRequestStatus({loading: false, status:true, data: res.data}) + }).catch(error => { + setRequestStatus({loading: false, status:false, data: []}) + }) + } + + useEffect(()=>{ + // little checker for the validity of the token + if (token==null || token?.length != 64) { + return setRequestStatus({loading: false, status:false, data: []}); + } + verifyResetPwd() + },[]) + return ( + <> + +
+
+ + wrenchboard + +
+
+ {requestStatus.loading ? + + : + !requestStatus.loading && requestStatus.status ? +
+ {linkSuccess == null ? + <> +
+

+ Password Reset +

+ + Enter a new password to reset + + + We'll send an email to confirm reset + +
+
+

+ Must include a special, numeric, uppercase and lowercase character +

+
+ navigate("/login")} + /> + + : + + } +
+ : +
+ +
+ } +
+
+
+ + ); +}; + +export default VerifyPassword; + +const SuccessfulComponent = ({ + onSubmit, + navigateHandler, + showPassword, + onClick, + password, + confirmPassword, + handlePassword, + msgErr, + loader, +}) => ( +
+ {/* INPUT */} +
+ +
+
+ +
+ {msgErr && ( +
+ {msgErr} +
+ )} +
+ +
+
+ +
+
+);