From 4239e39ce78fae3bfcc87df50c49aef73d650407 Mon Sep 17 00:00:00 2001 From: YasamanKahdooei Date: Sun, 14 Apr 2024 15:12:09 +0330 Subject: [PATCH] part of Verify Added --- app/After/page.tsx | 9 ++ components/Verify/VerifyInput.tsx | 134 +++++++++++++++++++++++------- utils/commonApi.ts | 17 ++-- utils/variables.js | 14 ++++ 4 files changed, 141 insertions(+), 33 deletions(-) create mode 100644 app/After/page.tsx create mode 100644 utils/variables.js diff --git a/app/After/page.tsx b/app/After/page.tsx new file mode 100644 index 0000000..512ef84 --- /dev/null +++ b/app/After/page.tsx @@ -0,0 +1,9 @@ +import React from 'react' + +function page() { + return ( +
After Login
+ ) +} + +export default page \ No newline at end of file diff --git a/components/Verify/VerifyInput.tsx b/components/Verify/VerifyInput.tsx index 2848ad9..225998a 100644 --- a/components/Verify/VerifyInput.tsx +++ b/components/Verify/VerifyInput.tsx @@ -11,35 +11,39 @@ import { JwtPayload, jwtDecode } from "jwt-decode"; import { toast } from "sonner"; import { verifyCitizen } from "@/utils/authenticateApi"; import CountdownTimer from "./CountDown"; +import { resendOtp } from "@/utils/commonApi"; +import { appConstants } from "../../utils/variables"; export const VerifyInput = () => { const [otp, setOtp] = useState(""); const space = "\xa0"; const router = useRouter(); - const userData = useData((state) => state.data); - const [VerifyRole, setVerifyRole] = useState(""); const saveToken = useToken((state) => state.saveToken); const [isShow, setIsShow] = useState(true); - + const userData = useData((state) => state.data); + const [VerifyRole, setVerifyRole] = useState(""); const CitizenVerifyMutation = useMutation({ mutationFn: verifyCitizen, onSuccess: (res) => { saveToken(true); const decodedToken = jwtDecode(res.jwtToken); - setCookie("Sootzani_Token", res.jwtToken); - setCookie("Sootzani_Refresh_Token", res.refreshToken); + setCookie("Shahrbin_Token", res.jwtToken); + setCookie("Shahrbin_Refresh_Token", res.refreshToken); const userRole = decodedToken[ "http://schemas.microsoft.com/ws/2008/06/identity/claims/role" ]; - localStorage.setItem("Sootzani_Role", userRole); - const justLogin = localStorage.getItem("Sootzani_Login_State"); + localStorage.setItem("Shahrbin_Role", userRole); + const justLogin = localStorage.getItem("Shahrbin_Login_State"); if (justLogin) { - router.push("/Account/Complaints"); - localStorage.removeItem("Sootzani_Login_State"); + // router.push("/Account/Complaints"); + router.push("/After"); + localStorage.removeItem("Shahrbin_Login_State"); + console.log("justLogin"); } else { - router.push("/RequestStep1"); + console.log("Dont justLogin"); + // router.push("/RequestStep1"); } }, onError: (err: any | string) => { @@ -49,20 +53,64 @@ export const VerifyInput = () => { }, }); - const handelVerify = () => { - const payload = { - otpToken: localStorage.getItem("verificationToken"), - verificationCode: otp, - }; - CitizenVerifyMutation.mutate(payload); - }; - // useEffect(() => { // useData.persist.rehydrate(); // }, []); + // SH.... + const resendOtpMutation = useMutation({ + mutationKey: ["resendOtp"], + mutationFn: resendOtp, + onSuccess: (res: any) => {}, + onError: (err: any) => { + if (err.response.status === 428) { + console.log(err.response); + localStorage.setItem( + appConstants.SH_CT_OTP_TOKEN, + err.response.data.data + ); + // toast("کد تایید مجدد ارسال شد.", { type: "info" }); + toast("کد تایید مجدد ارسال شد.", { + className: "!bg-yellow !text-black", + }); + + localStorage.removeItem("countdownTime"); + localStorage.removeItem("CountDownCompleted"); + setIsShow(true); + } else { + // toast(err.response.data.message, { type: "error" }); + toast(<>{err.response.data.message}, { + className: "!bg-red-500 !text-white", + }); + } + }, + }); + + const handelResendOtp = () => { + console.log(11); + resendOtpMutation.mutate({ + otpToken: localStorage.getItem(appConstants.SH_CT_OTP_TOKEN), + }); + }; + useEffect(() => { + console.log(otp); + if (otp.length == 6) { + if (localStorage.getItem("CountDownCompleted")) { + const otpTokenString = localStorage.getItem( + appConstants.SH_CT_OTP_TOKEN + ); + if (otpTokenString !== null) { + resendOtpMutation.mutate({ + otpToken: JSON.parse(otpTokenString), + }); + } + } else { + console.log(2); + // makeRequest(); + } + const payload = { otpToken: localStorage.getItem("verificationToken"), verificationCode: otp, @@ -71,15 +119,46 @@ export const VerifyInput = () => { } }, [otp]); - // SH.... + // Soot + // const handelVerify = () => { + // const payload = { + // // otpToken: localStorage.getItem("verificationToken"), + // otpToken: localStorage.getItem(appConstants.SH_CT_OTP_TOKEN), + // verificationCode: otp, + // }; + // CitizenVerifyMutation.mutate(payload); + // }; - - const handelResendOtp = () => { - console.log(11); - // resendOtpMutation.mutate({ - // otpToken: localStorage.getItem(appConstants.SH_CT_OTP_TOKEN), - // }); - }; + // Sha + // const handleVerify = async (e) => { + // const payload = { + // verificationCode: otp, + // otpToken: localStorage.getItem(appConstants.SH_CT_OTP_TOKEN), + // }; + // try { + // const { success, data } = await api.Authenticate({ + // // tail: state.type === "signin" ? "Verify" : "RequestToken", + // tail: " ", + // payload, + // method: httpMethods.post, + // }); + // if (success) { + // // if (state.type === "signin") onSignInSuccess(data); + // // else + // // navigate(appRoutes.resetpass, { + // // state: { token: data, userName: state.phoneNumber }, + // // }); + // console.log(data); + // onSignInSuccess(data); + // if (localStorage.getItem(appConstants.SH_CT_LOGIN_URL)) { + // navigate(localStorage.getItem(appConstants.SH_CT_LOGIN_URL)); + // localStorage.removeItem(appConstants.SH_CT_LOGIN_URL); + // } else { + // navigate(appRoutes.menu); + // } + // } + // } catch (err) {} + // }; return ( <> @@ -118,10 +197,9 @@ export const VerifyInput = () => {
{" "} setIsShow(state)} + setIsShow={(state: any) => setIsShow(state)} isShow={isShow} /> - {!isShow && (