1
1
'use client' ;
2
2
3
- import { useEffect , useState } from 'react' ;
3
+ import { useEffect , useState , useCallback } from 'react' ;
4
4
import { useRouter } from 'next/navigation' ;
5
5
import { toast } from 'sonner' ;
6
6
import { signIn } from 'next-auth/react' ;
@@ -36,18 +36,19 @@ import { signInAction, generateOTPLink } from './action';
36
36
const SignInOTP = ( { email} ) => {
37
37
const [ otp , setOtp ] = useState ( '' ) ;
38
38
const [ disabled , setDisabled ] = useState ( false ) ;
39
+ const [ url , setUrl ] = useState ( null ) ;
39
40
const router = useRouter ( ) ;
40
41
41
- const onSubmit = async ( data ) => {
42
+ const onSubmit = useCallback ( async ( code , email ) => {
42
43
setDisabled ( true ) ;
43
44
44
- const parsedCode = signInOTPSchema . safeParse ( { code : data } ) ;
45
+ const parsedCode = signInOTPSchema . safeParse ( { code : code } ) ;
45
46
const parsedEmail = signInSchema . safeParse ( { email : email } ) ;
46
47
47
48
if ( parsedCode ?. success && parsedEmail ?. success ) {
48
49
const url = await generateOTPLink ( parsedCode . data . code , parsedEmail . data . email ) ;
49
50
if ( url ) {
50
- router . push ( url ) ;
51
+ setUrl ( url ) ;
51
52
} else {
52
53
toast . error ( 'This is not a valid code! Please try again.' ) ;
53
54
}
@@ -56,14 +57,21 @@ const SignInOTP = ({email}) => {
56
57
}
57
58
58
59
setDisabled ( false ) ;
59
- } ;
60
+ } , [ ] ) ;
61
+
62
+ useEffect ( ( ) => {
63
+ if ( url ) {
64
+ setUrl ( null ) ;
65
+ router . push ( url ) ;
66
+ }
67
+ } , [ url ] ) ;
60
68
61
69
return (
62
70
< InputOTP
63
71
maxLength = { 6 }
64
72
value = { otp }
65
73
onChange = { setOtp }
66
- onComplete = { onSubmit }
74
+ onComplete = { ( data ) => onSubmit ( data , email ) }
67
75
disabled = { disabled }
68
76
containerClassName = 'flex-col sm:flex-row justify-center'
69
77
>
@@ -116,7 +124,7 @@ const SignInSendAgain = ({ email, onClick }) => {
116
124
< CardContent className = 'flex flex-col gap-4' >
117
125
< Divider text = 'or' />
118
126
< div className = 'space-y-2 text-center' >
119
- < p className = 'text-sm text-muted-foreground' > Enter the 8 -digit code sent to your email</ p >
127
+ < p className = 'text-sm text-muted-foreground' > Enter the 6 -digit code sent to your email</ p >
120
128
< SignInOTP email = { email } />
121
129
</ div >
122
130
</ CardContent >
0 commit comments