Skip to content

Commit 63d7df5

Browse files
committed
feat(auth): refactor email OTP handling in LoginPage and VerifyPage using useMutation for improved error handling and loading state management
1 parent 257e118 commit 63d7df5

File tree

2 files changed

+43
-35
lines changed

2 files changed

+43
-35
lines changed

src/modules/auth/pages/LoginPage.tsx

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,23 @@ const LoginPage = () => {
5454
onClose: closeDemoWarning,
5555
} = useDisclosure();
5656

57+
const emailOtpMutation = useMutation({
58+
mutationFn: async (email: string) => {
59+
const { data, error } = await authClient.emailOtp.sendVerificationOtp({
60+
email,
61+
type: 'sign-in',
62+
});
63+
if (error) {
64+
throw new Error(error.message);
65+
}
66+
return data;
67+
},
68+
});
69+
5770
const loginForm = useForm<{ email: string }>({
5871
onValidSubmit: async ({ email }) => {
5972
try {
60-
const { error } = await authClient.emailOtp.sendVerificationOtp({
61-
email,
62-
type: 'sign-in',
63-
});
64-
if (error) {
65-
showError(
66-
error.code
67-
? t(`auth:errorCode.${String(error.code)}`)
68-
: t('login:feedbacks.error')
69-
);
70-
return;
71-
}
73+
await emailOtpMutation.mutateAsync(email);
7274
router.navigate({
7375
pathname: '/verify',
7476
params: {
@@ -160,7 +162,7 @@ const LoginPage = () => {
160162
/>
161163
<Button
162164
onPress={() => loginForm.submit()}
163-
isLoading={false}
165+
isLoading={emailOtpMutation.isLoading}
164166
variant="@primary"
165167
size="lg"
166168
full
@@ -177,7 +179,7 @@ const LoginPage = () => {
177179
<Button
178180
variant="outline"
179181
full
180-
// isLoading={social.isLoading}
182+
isLoading={social.isLoading}
181183
onPress={handleGitHubLogin}
182184
>
183185
{t('login:actions.loginWithGitHub')}

src/modules/auth/pages/VerifyPage.tsx

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { useEffect, useRef } from 'react';
33

44
import { Formiz, useForm } from '@formiz/core';
5+
import { useMutation } from '@tanstack/react-query';
56
import { useLocalSearchParams, useRouter } from 'expo-router';
67
import { ArrowLeft } from 'lucide-react-native';
78
import { useTranslation } from 'react-i18next';
@@ -24,30 +25,35 @@ const VerifyPage = () => {
2425
const { t } = useTranslation();
2526
const { showSuccess } = useToast();
2627

28+
const signInMutation = useMutation({
29+
mutationFn: async ({ code }: { code: string }) => {
30+
const { error } = await authClient.signIn.emailOtp({
31+
email,
32+
otp: code,
33+
});
34+
if (error) {
35+
codeForm.setValues({ code: '' });
36+
codeForm.setErrors({
37+
code: t('login:validation.error'),
38+
});
39+
throw error;
40+
}
41+
const session = await authClient.getSession();
42+
if (session.data?.user) {
43+
useSessionStore.getState().setIsAuthentificated(true);
44+
useSessionStore
45+
.getState()
46+
.setIsOnboarded(!!session.data.user.onboardedAt);
47+
}
48+
showSuccess(t('login:validation.success'));
49+
},
50+
});
51+
2752
// Formiz instance for the 6-digit code
2853
const codeForm = useForm<{ code: string }>({
2954
onValidSubmit: async ({ code }) => {
3055
try {
31-
const { error } = await authClient.signIn.emailOtp({
32-
email,
33-
otp: code,
34-
});
35-
if (error) {
36-
codeForm.setValues({ code: '' });
37-
codeForm.setErrors({
38-
code: t('login:validation.error'),
39-
});
40-
return;
41-
}
42-
const session = await authClient.getSession();
43-
if (session.data?.user) {
44-
useSessionStore.getState().setIsAuthentificated(true);
45-
useSessionStore
46-
.getState()
47-
.setIsOnboarded(!!session.data.user.onboardedAt);
48-
}
49-
50-
showSuccess(t('login:validation.success'));
56+
signInMutation.mutate({ code });
5157
// TODO: navigate into the app
5258
} catch (err) {
5359
codeForm.setValues({ code: '' });
@@ -155,7 +161,7 @@ const VerifyPage = () => {
155161
mt="lg"
156162
variant="@primary"
157163
size="lg"
158-
isLoading={codeForm.isValidating}
164+
isLoading={signInMutation.isLoading}
159165
onPress={() => codeForm.submit()}
160166
>
161167
{t('login:verification.confirm')}

0 commit comments

Comments
 (0)