diff --git a/src/pages/auth/components/LoginStep1.tsx b/src/pages/auth/components/LoginStep1.tsx index 8303a11..9626e4d 100644 --- a/src/pages/auth/components/LoginStep1.tsx +++ b/src/pages/auth/components/LoginStep1.tsx @@ -18,7 +18,7 @@ type LoginStep1FormType = { const LoginStep1: FC = () => { const { t } = useTranslation('global') - const { setPhone, phone, setStepLogin, setSlug } = useAuthStore() + const { setPhone, phone, setStepLogin, setSlug, setOtp } = useAuthStore() const loginWithOtp = useLoginWithOtp() const formik = useFormik({ @@ -36,7 +36,8 @@ const LoginStep1: FC = () => { setPhone(values.phone) setSlug(values.slug) loginWithOtp.mutate(values, { - onSuccess() { + onSuccess(data) { + setOtp(data?.data?.code || '') setStepLogin(2) toast.success(t('auth.otp_sent')) }, diff --git a/src/pages/auth/components/LoginStep2.tsx b/src/pages/auth/components/LoginStep2.tsx index 404820a..9d798b3 100644 --- a/src/pages/auth/components/LoginStep2.tsx +++ b/src/pages/auth/components/LoginStep2.tsx @@ -16,13 +16,13 @@ import { setToken, setRefreshToken } from '../../../config/func' const LoginStep2: FC = () => { const { t } = useTranslation('global') - const { phone, setStepLogin, slug } = useAuthStore() + const { phone, setStepLogin, slug, otp } = useAuthStore() const { value } = useCountDown(2, true) const otpVerify = useOtpVerify() const formik = useFormik<{ otp: string }>({ initialValues: { - otp: '' + otp: otp }, validationSchema: Yup.object({ otp: Yup.string() diff --git a/src/pages/auth/hooks/useAuthData.tsx b/src/pages/auth/hooks/useAuthData.tsx index 39fbba0..0eb0d53 100644 --- a/src/pages/auth/hooks/useAuthData.tsx +++ b/src/pages/auth/hooks/useAuthData.tsx @@ -1,6 +1,6 @@ import { useMutation } from '@tanstack/react-query'; import * as api from '../service/AuthService' -import type { CheckHasAccountPhoneType, CheckHasAccountType, LoginWithOtpType, LoginWithPasswordType, OtpVerifyType, RegisterType } from '../types/AuthTypes'; +import type { CheckHasAccountPhoneType, CheckHasAccountType, LoginWithOtpType, LoginWithOtpResponse, LoginWithPasswordType, OtpVerifyType, RegisterType } from '../types/AuthTypes'; export const useLoginWithPassword = () => { return useMutation({ @@ -9,7 +9,7 @@ export const useLoginWithPassword = () => { }; export const useLoginWithOtp = () => { - return useMutation({ + return useMutation({ mutationFn: (variables: LoginWithOtpType) => api.loginWithOtp(variables), }); }; diff --git a/src/pages/auth/service/AuthService.ts b/src/pages/auth/service/AuthService.ts index 9337f02..350b3c3 100644 --- a/src/pages/auth/service/AuthService.ts +++ b/src/pages/auth/service/AuthService.ts @@ -3,6 +3,7 @@ import { type CheckHasAccountPhoneType, type CheckHasAccountType, type LoginWithOtpType, + type LoginWithOtpResponse, type LoginWithPasswordType, type OtpVerifyType, type RefreshTokenType, @@ -15,8 +16,13 @@ export const loginWithPassword = async (params: LoginWithPasswordType) => { return data; }; -export const loginWithOtp = async (params: LoginWithOtpType) => { - const { data } = await axios.post(`/admin/auth/otp/request`, params); +export const loginWithOtp = async ( + params: LoginWithOtpType +): Promise => { + const { data } = await axios.post( + `/admin/auth/otp/request`, + params + ); return data; }; diff --git a/src/pages/auth/store/AuthStore.ts b/src/pages/auth/store/AuthStore.ts index 9f82f02..8337433 100644 --- a/src/pages/auth/store/AuthStore.ts +++ b/src/pages/auth/store/AuthStore.ts @@ -18,4 +18,8 @@ export const useAuthStore = create((set) => ({ setStepLogin(value) { set({ stepLogin: value }); }, + otp: "", + setOtp(value) { + set({ otp: value }); + }, })); diff --git a/src/pages/auth/types/AuthTypes.ts b/src/pages/auth/types/AuthTypes.ts index a7f6b3d..5204cd6 100644 --- a/src/pages/auth/types/AuthTypes.ts +++ b/src/pages/auth/types/AuthTypes.ts @@ -17,6 +17,8 @@ export type AuthStoreType = { setEmail: (value: string) => void; stepLogin: number; setStepLogin: (value: number) => void; + otp: string; + setOtp: (value: string) => void; }; export type LoginWithPasswordType = XOR< @@ -74,3 +76,9 @@ export type RefreshTokenResponseData = { }; export type RefreshTokenResponse = IResponse; + +export type LoginWithOtpResponseData = { + code: string; +}; + +export type LoginWithOtpResponse = IResponse;