2
2
import { useEffect , useRef } from 'react' ;
3
3
4
4
import { Formiz , useForm } from '@formiz/core' ;
5
+ import { useMutation } from '@tanstack/react-query' ;
5
6
import { useLocalSearchParams , useRouter } from 'expo-router' ;
6
7
import { ArrowLeft } from 'lucide-react-native' ;
7
8
import { useTranslation } from 'react-i18next' ;
@@ -24,30 +25,35 @@ const VerifyPage = () => {
24
25
const { t } = useTranslation ( ) ;
25
26
const { showSuccess } = useToast ( ) ;
26
27
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
+
27
52
// Formiz instance for the 6-digit code
28
53
const codeForm = useForm < { code : string } > ( {
29
54
onValidSubmit : async ( { code } ) => {
30
55
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 } ) ;
51
57
// TODO: navigate into the app
52
58
} catch ( err ) {
53
59
codeForm . setValues ( { code : '' } ) ;
@@ -155,7 +161,7 @@ const VerifyPage = () => {
155
161
mt = "lg"
156
162
variant = "@primary"
157
163
size = "lg"
158
- isLoading = { codeForm . isValidating }
164
+ isLoading = { signInMutation . isLoading }
159
165
onPress = { ( ) => codeForm . submit ( ) }
160
166
>
161
167
{ t ( 'login:verification.confirm' ) }
0 commit comments