login with fill otp

This commit is contained in:
hamid zarghami
2025-12-14 15:12:59 +03:30
parent adad5d4429
commit afa0bbf3fb
6 changed files with 27 additions and 8 deletions
+3 -2
View File
@@ -18,7 +18,7 @@ type LoginStep1FormType = {
const LoginStep1: FC = () => { const LoginStep1: FC = () => {
const { t } = useTranslation('global') const { t } = useTranslation('global')
const { setPhone, phone, setStepLogin, setSlug } = useAuthStore() const { setPhone, phone, setStepLogin, setSlug, setOtp } = useAuthStore()
const loginWithOtp = useLoginWithOtp() const loginWithOtp = useLoginWithOtp()
const formik = useFormik<LoginStep1FormType>({ const formik = useFormik<LoginStep1FormType>({
@@ -36,7 +36,8 @@ const LoginStep1: FC = () => {
setPhone(values.phone) setPhone(values.phone)
setSlug(values.slug) setSlug(values.slug)
loginWithOtp.mutate(values, { loginWithOtp.mutate(values, {
onSuccess() { onSuccess(data) {
setOtp(data?.data?.code || '')
setStepLogin(2) setStepLogin(2)
toast.success(t('auth.otp_sent')) toast.success(t('auth.otp_sent'))
}, },
+2 -2
View File
@@ -16,13 +16,13 @@ import { setToken, setRefreshToken } from '../../../config/func'
const LoginStep2: FC = () => { const LoginStep2: FC = () => {
const { t } = useTranslation('global') const { t } = useTranslation('global')
const { phone, setStepLogin, slug } = useAuthStore() const { phone, setStepLogin, slug, otp } = useAuthStore()
const { value } = useCountDown(2, true) const { value } = useCountDown(2, true)
const otpVerify = useOtpVerify() const otpVerify = useOtpVerify()
const formik = useFormik<{ otp: string }>({ const formik = useFormik<{ otp: string }>({
initialValues: { initialValues: {
otp: '' otp: otp
}, },
validationSchema: Yup.object({ validationSchema: Yup.object({
otp: Yup.string() otp: Yup.string()
+2 -2
View File
@@ -1,6 +1,6 @@
import { useMutation } from '@tanstack/react-query'; import { useMutation } from '@tanstack/react-query';
import * as api from '../service/AuthService' 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 = () => { export const useLoginWithPassword = () => {
return useMutation({ return useMutation({
@@ -9,7 +9,7 @@ export const useLoginWithPassword = () => {
}; };
export const useLoginWithOtp = () => { export const useLoginWithOtp = () => {
return useMutation({ return useMutation<LoginWithOtpResponse, Error, LoginWithOtpType>({
mutationFn: (variables: LoginWithOtpType) => api.loginWithOtp(variables), mutationFn: (variables: LoginWithOtpType) => api.loginWithOtp(variables),
}); });
}; };
+8 -2
View File
@@ -3,6 +3,7 @@ import {
type CheckHasAccountPhoneType, type CheckHasAccountPhoneType,
type CheckHasAccountType, type CheckHasAccountType,
type LoginWithOtpType, type LoginWithOtpType,
type LoginWithOtpResponse,
type LoginWithPasswordType, type LoginWithPasswordType,
type OtpVerifyType, type OtpVerifyType,
type RefreshTokenType, type RefreshTokenType,
@@ -15,8 +16,13 @@ export const loginWithPassword = async (params: LoginWithPasswordType) => {
return data; return data;
}; };
export const loginWithOtp = async (params: LoginWithOtpType) => { export const loginWithOtp = async (
const { data } = await axios.post(`/admin/auth/otp/request`, params); params: LoginWithOtpType
): Promise<LoginWithOtpResponse> => {
const { data } = await axios.post<LoginWithOtpResponse>(
`/admin/auth/otp/request`,
params
);
return data; return data;
}; };
+4
View File
@@ -18,4 +18,8 @@ export const useAuthStore = create<AuthStoreType>((set) => ({
setStepLogin(value) { setStepLogin(value) {
set({ stepLogin: value }); set({ stepLogin: value });
}, },
otp: "",
setOtp(value) {
set({ otp: value });
},
})); }));
+8
View File
@@ -17,6 +17,8 @@ export type AuthStoreType = {
setEmail: (value: string) => void; setEmail: (value: string) => void;
stepLogin: number; stepLogin: number;
setStepLogin: (value: number) => void; setStepLogin: (value: number) => void;
otp: string;
setOtp: (value: string) => void;
}; };
export type LoginWithPasswordType = XOR< export type LoginWithPasswordType = XOR<
@@ -74,3 +76,9 @@ export type RefreshTokenResponseData = {
}; };
export type RefreshTokenResponse = IResponse<RefreshTokenResponseData>; export type RefreshTokenResponse = IResponse<RefreshTokenResponseData>;
export type LoginWithOtpResponseData = {
code: string;
};
export type LoginWithOtpResponse = IResponse<LoginWithOtpResponseData>;