diff --git a/src/app/auth/page.tsx b/src/app/auth/page.tsx index c1c41a3..3ae75cf 100644 --- a/src/app/auth/page.tsx +++ b/src/app/auth/page.tsx @@ -14,6 +14,7 @@ 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 @@ -34,8 +35,18 @@ function AuthIndex({ }: Props) { const { run: runUserExistCheck } = useCheckUserExistsLazy() const { run: runOtpRequest } = useOtpRequest(); const { run: runOtpValidation } = useOtpValidation(); + const { run: runResetPassword } = useResetPassword(); - + const resetStates = () => { + setNumber(''); + setPassword(''); + setPasswordRepeat(''); + setOtp(''); + setShowPassword(false); + setShowPasswordRepeat(false); + setRememberMe(false); + stop(); + } useEffect(() => { if (isAuthenticated) { @@ -130,7 +141,17 @@ function AuthIndex({ }: Props) { } else if (step == AUTH_STEP.ENTER_RESET_PASSWORD) { console.log("Password changed") - setStep(AUTH_STEP.ENTER_NUMBER) + 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 { diff --git a/src/hooks/auth/useResetPassword.ts b/src/hooks/auth/useResetPassword.ts new file mode 100644 index 0000000..de9927a --- /dev/null +++ b/src/hooks/auth/useResetPassword.ts @@ -0,0 +1,20 @@ +import { useQueryClient } from '@tanstack/react-query' +import { useAuthStore } from '@/zustand/authStore' +import { resetPassword, ResetPasswordRequestModel } from '@/lib/api/auth/reset-password'; + +export const useResetPassword = () => +{ + const queryClient = useQueryClient() + + const run = (model: ResetPasswordRequestModel) => + queryClient.fetchQuery({ + queryKey: ['resetPassword', model], + queryFn: () => resetPassword(model), + }).then(() => { + useAuthStore.getState().logout(); + }).catch((ex) => { + throw ex; + }) + + return { run } +} diff --git a/src/lib/api/auth/reset-password.ts b/src/lib/api/auth/reset-password.ts new file mode 100644 index 0000000..059519d --- /dev/null +++ b/src/lib/api/auth/reset-password.ts @@ -0,0 +1,12 @@ +import { api } from "../axiosInstance"; + +export type ResetPasswordRequestModel = { + phone: string, + otp: string, + newPassword: string, +} + +export const resetPassword = async (model: ResetPasswordRequestModel) => { + const res = await api.post('/reset-password', model) + return res.data +}