add: reset password functionality
This commit is contained in:
+23
-2
@@ -14,6 +14,7 @@ import { useSignup } from '@/hooks/auth/useSignup';
|
|||||||
import { useCheckUserExistsLazy } from '@/hooks/auth/useCheckUserExists';
|
import { useCheckUserExistsLazy } from '@/hooks/auth/useCheckUserExists';
|
||||||
import { useOtpRequest } from '@/hooks/auth/useOtpRequest';
|
import { useOtpRequest } from '@/hooks/auth/useOtpRequest';
|
||||||
import { useOtpValidation } from '@/hooks/auth/useOtpValidation';
|
import { useOtpValidation } from '@/hooks/auth/useOtpValidation';
|
||||||
|
import { useResetPassword } from '@/hooks/auth/useResetPassword';
|
||||||
|
|
||||||
type Props = object
|
type Props = object
|
||||||
|
|
||||||
@@ -34,8 +35,18 @@ function AuthIndex({ }: Props) {
|
|||||||
const { run: runUserExistCheck } = useCheckUserExistsLazy()
|
const { run: runUserExistCheck } = useCheckUserExistsLazy()
|
||||||
const { run: runOtpRequest } = useOtpRequest();
|
const { run: runOtpRequest } = useOtpRequest();
|
||||||
const { run: runOtpValidation } = useOtpValidation();
|
const { run: runOtpValidation } = useOtpValidation();
|
||||||
|
const { run: runResetPassword } = useResetPassword();
|
||||||
|
|
||||||
|
const resetStates = () => {
|
||||||
|
setNumber('');
|
||||||
|
setPassword('');
|
||||||
|
setPasswordRepeat('');
|
||||||
|
setOtp('');
|
||||||
|
setShowPassword(false);
|
||||||
|
setShowPasswordRepeat(false);
|
||||||
|
setRememberMe(false);
|
||||||
|
stop();
|
||||||
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (isAuthenticated) {
|
if (isAuthenticated) {
|
||||||
@@ -130,7 +141,17 @@ function AuthIndex({ }: Props) {
|
|||||||
}
|
}
|
||||||
else if (step == AUTH_STEP.ENTER_RESET_PASSWORD) {
|
else if (step == AUTH_STEP.ENTER_RESET_PASSWORD) {
|
||||||
console.log("Password changed")
|
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) {
|
else if (step == AUTH_STEP.ENTER_NEW_PASSWORD) {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -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 }
|
||||||
|
}
|
||||||
@@ -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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user