refactor: remove console log and enhance error handling in signin and resend OTP actions
This commit is contained in:
@@ -8,8 +8,6 @@ import {
|
||||
} from '../_validation/signin-validation';
|
||||
|
||||
export async function SigninAction(request: SignInValidationType) {
|
||||
console.log(request);
|
||||
|
||||
const validRequest = signInValidationSchema.parse(request);
|
||||
|
||||
const { data } = await fetchPostSignin(validRequest);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { useMutation } from '@tanstack/react-query';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { toast } from 'sonner';
|
||||
import { SigninAction } from '../_actions/signin-action';
|
||||
import { useFormSignin } from './use-form-signin';
|
||||
|
||||
@@ -8,11 +9,20 @@ export function usePostSignin(form: ReturnType<typeof useFormSignin>) {
|
||||
|
||||
return useMutation({
|
||||
mutationFn: SigninAction,
|
||||
|
||||
onSuccess: () => {
|
||||
router.push('/');
|
||||
},
|
||||
onError: () => {
|
||||
onError: (error, variables) => {
|
||||
form.resetField('password');
|
||||
|
||||
if (error.message.includes('not active')) {
|
||||
setTimeout(() => {
|
||||
router.push(`/verification?ref=${variables.email}`);
|
||||
}, 750);
|
||||
}
|
||||
|
||||
toast.error(error.message);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -19,13 +19,13 @@ export async function resendOTPAction(request: ResendOTPValidationType) {
|
||||
|
||||
if (!isCapchaValid) throw new Error('Failed to verify captcha');
|
||||
|
||||
const { data } = await fetcher.POST('/v1/auth/send-otp', {
|
||||
const { data, error } = await fetcher.POST('/v1/auth/send-otp', {
|
||||
body: {
|
||||
email: validRequest.email,
|
||||
},
|
||||
});
|
||||
|
||||
console.log(data);
|
||||
if (error) throw new Error(error.message);
|
||||
|
||||
return data?.message;
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ export function VerificationTabs() {
|
||||
)}
|
||||
onClick={() => setActiveTab('resend')}
|
||||
>
|
||||
Kirim Ulang OTP
|
||||
Resend OTP
|
||||
</button>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -1,13 +1,18 @@
|
||||
import { useMutation } from '@tanstack/react-query';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { toast } from 'sonner';
|
||||
import { resendOTPAction } from '../_actions/resend-otp-action';
|
||||
import { useFormResendOTP } from './use-form-resend-otp';
|
||||
|
||||
export function usePostResendOTP(form: ReturnType<typeof useFormResendOTP>) {
|
||||
const router = useRouter();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: resendOTPAction,
|
||||
onSuccess: () => router.replace('/verification?success=true'),
|
||||
onSuccess: ({ message }) => {
|
||||
form.reset();
|
||||
toast.success(message);
|
||||
},
|
||||
onError: ({ message }) => {
|
||||
form.reset();
|
||||
toast.error(message);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user