From f4f35445975f4f7c12cb5e0aff7e2f3074f5d110 Mon Sep 17 00:00:00 2001 From: Mahyar Khanbolooki Date: Thu, 3 Jul 2025 19:09:03 +0330 Subject: [PATCH] add: bring back previously disabled api calls --- src/app/auth/page.tsx | 155 +++++++++--------- .../auth/components/AuthFormWrapper.tsx | 2 +- 2 files changed, 83 insertions(+), 74 deletions(-) diff --git a/src/app/auth/page.tsx b/src/app/auth/page.tsx index cf61087..87ed91e 100644 --- a/src/app/auth/page.tsx +++ b/src/app/auth/page.tsx @@ -10,12 +10,12 @@ import { AUTH_PAGE_ELEMENT, AUTH_STEP } from '@/enums'; import { useCountdown } from '@/hooks/useCountdown'; import StepNewPassword from '@/features/auth/components/StepNewPassword'; import AuthFormWrapper from '@/features/auth/components/AuthFormWrapper'; -// import { useLogin } from '@/hooks/auth/useLogin'; -// import { useSignup } from '@/hooks/auth/useSignup'; -// import { useCheckUserExistsLazy } from '@/hooks/auth/useCheckUserExists'; -// import { useOtpRequest } from '@/hooks/auth/useOtpRequest'; -// import { useOtpValidation } from '@/hooks/auth/useOtpValidation'; -// import { useResetPassword } from '@/hooks/auth/useResetPassword'; +import { useLogin } from '@/hooks/auth/useLogin'; +import { useSignup } from '@/hooks/auth/useSignup'; +import { useCheckUserExistsLazy } from '@/hooks/auth/useCheckUserExists'; +import { useOtpRequest } from '@/hooks/auth/useOtpRequest'; +import { useOtpValidation } from '@/hooks/auth/useOtpValidation'; +import { useResetPassword } from '@/hooks/auth/useResetPassword'; type Props = object @@ -32,12 +32,12 @@ function AuthIndex({ }: Props) { const { timerRunning, secondsLeft, restart, stop } = useCountdown(step === AUTH_STEP.ENTER_OTP); const isAuthenticated = useAuthStore((state) => state.isAuthenticated) - // const loginMutation = useLogin() - // const signupMutation = useSignup() - // const { run: runUserExistCheck } = useCheckUserExistsLazy() - // const { run: runOtpRequest } = useOtpRequest(); - // const { run: runOtpValidation } = useOtpValidation(); - // const { run: runResetPassword } = useResetPassword(); + const loginMutation = useLogin() + const signupMutation = useSignup() + const { run: runUserExistCheck } = useCheckUserExistsLazy() + const { run: runOtpRequest } = useOtpRequest(); + const { run: runOtpValidation } = useOtpValidation(); + const { run: runResetPassword } = useResetPassword(); const resetStates = () => { setNumber(''); @@ -59,7 +59,7 @@ function AuthIndex({ }: Props) { useEffect(() => { if (step === AUTH_STEP.ENTER_OTP) { console.log("REQUEST OTP") - // runOtpRequest(number); + runOtpRequest(number); } }, [step]); @@ -81,7 +81,7 @@ function AuthIndex({ }: Props) { const prev = otp.padEnd(6).split(''); prev[index] = e.target.value; const next = prev.join(''); - if(otp.length >= 6 && next.trim().length >= otp.length) return; + if (otp.length >= 6 && next.trim().length >= otp.length) return; setOtp(next.trimEnd()); } } @@ -97,82 +97,91 @@ function AuthIndex({ }: Props) { setShowPasswordRepeat((state) => !state); } else if (target.id === AUTH_PAGE_ELEMENT.RESET_PASSWORD) { + setShowPassword(false); + setShowPasswordRepeat(false); + setPassword(''); + setPasswordRepeat(''); setStep(() => AUTH_STEP.ENTER_OTP) } else if (target.id === AUTH_PAGE_ELEMENT.RESEND_OTP) { if (!timerRunning) { try { - // await runOtpRequest(number); + await runOtpRequest(number); restart(); } catch { - + console.error("Could not ask for otp") } } } }; const onSubmit = async (e: FormEvent) => { + console.log(step); e.preventDefault(); - setIsPending(true); - if (step == AUTH_STEP.ENTER_NUMBER) { + try { + setIsPending(true); + if (step == AUTH_STEP.ENTER_NUMBER) { - if (true) { - setStep(AUTH_STEP.ENTER_PASSWORD) - } else { - setStep(AUTH_STEP.ENTER_OTP); + if (await runUserExistCheck(number)) { + setStep(AUTH_STEP.ENTER_PASSWORD) + } else { + setStep(AUTH_STEP.ENTER_OTP); + } } + else if (step == AUTH_STEP.ENTER_PASSWORD) { + try { + await loginMutation.mutateAsync({ phone: number, password }) + console.log("Logged in") + redirect("/") + } + catch (e) { + console.error("Wrong credentials: ", e) + } + } + else if (step == AUTH_STEP.ENTER_OTP) { + if (!await runOtpValidation({ phone: number, otp })) { + setOtp(''); + console.error('Wrong otp'); + } + stop(); + console.log(await runUserExistCheck(number)); + if (await runUserExistCheck(number)) { + console.log("1") + setStep(AUTH_STEP.ENTER_RESET_PASSWORD) + } else { + console.log("2") + setStep(AUTH_STEP.ENTER_NEW_PASSWORD) + } + } + else if (step == AUTH_STEP.ENTER_RESET_PASSWORD) { + console.log("Password changed") + try { + await runResetPassword({ + phone: number, + newPassword: password, + otp + }) + resetStates(); + setStep(AUTH_STEP.ENTER_NUMBER) + } catch (ex) { + console.error('Password reset failed: ', ex) + } + } + else if (step == AUTH_STEP.ENTER_NEW_PASSWORD) { + try { + await signupMutation.mutateAsync({ phone: number, password }) + console.log("Signed up") + redirect("/") + } + catch (e) { + console.error("Could not signup: ", e) + } + } + } catch (ex) { + console.error(ex) + } finally { + setIsPending(false); } - else if (step == AUTH_STEP.ENTER_PASSWORD) { - try { - // await loginMutation.mutateAsync({ phone: number, password }) - useAuthStore.getState().isAuthenticated = true; - console.log("Logged in") - redirect("/") - } - catch (e) { - console.log("Wrong credentials: ", e) - } - } - else if (step == AUTH_STEP.ENTER_OTP) { - // if (!await runOtpValidation({ phone: number, otp })) { - // setOtp(''); - // console.log('Wrong otp'); - // } - setOtp(''); - stop(); - // if (await runUserExistCheck(number)) { - // setStep(AUTH_STEP.ENTER_RESET_PASSWORD) - // } else { - // setStep(AUTH_STEP.ENTER_NEW_PASSWORD) - // } - setStep(AUTH_STEP.ENTER_NEW_PASSWORD) - } - else if (step == AUTH_STEP.ENTER_RESET_PASSWORD) { - console.log("Password changed") - try { - // await runResetPassword({ - // phone: number, - // newPassword: password, - // otp - // }) - resetStates(); - setStep(AUTH_STEP.ENTER_NUMBER) - } catch (ex) { - console.error('Password reset failed: ', ex) - } - } - else if (step == AUTH_STEP.ENTER_NEW_PASSWORD) { - try { - // await signupMutation.mutateAsync({ phone: number, password }) - useAuthStore.getState().isAuthenticated = true; - console.log("Signed up") - redirect("/") - } - catch (e) { - console.log("Could not signup: ", e) - } - } - setIsPending(false); } const stepMap = { diff --git a/src/features/auth/components/AuthFormWrapper.tsx b/src/features/auth/components/AuthFormWrapper.tsx index 33f6539..f2d44df 100644 --- a/src/features/auth/components/AuthFormWrapper.tsx +++ b/src/features/auth/components/AuthFormWrapper.tsx @@ -10,7 +10,7 @@ function AuthFormWrapper({ children, isPending, ...restProps }: Props) {
{children} - +
)