Skip to content

Commit af5cc31

Browse files
committed
Verification token delete error is fixed by preventing double page refresh.
1 parent c6495aa commit af5cc31

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/app/#/signin-form.js

+15-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client';
22

3-
import { useEffect, useState } from 'react';
3+
import { useEffect, useState, useCallback } from 'react';
44
import { useRouter } from 'next/navigation';
55
import { toast } from 'sonner';
66
import { signIn } from 'next-auth/react';
@@ -36,18 +36,19 @@ import { signInAction, generateOTPLink } from './action';
3636
const SignInOTP = ({email}) => {
3737
const [otp, setOtp] = useState('');
3838
const [disabled, setDisabled] = useState(false);
39+
const [url, setUrl] = useState(null);
3940
const router = useRouter();
4041

41-
const onSubmit = async (data) => {
42+
const onSubmit = useCallback(async (code, email) => {
4243
setDisabled(true);
4344

44-
const parsedCode = signInOTPSchema.safeParse({ code: data });
45+
const parsedCode = signInOTPSchema.safeParse({ code: code });
4546
const parsedEmail = signInSchema.safeParse({ email: email });
4647

4748
if (parsedCode?.success && parsedEmail?.success) {
4849
const url = await generateOTPLink(parsedCode.data.code, parsedEmail.data.email);
4950
if (url) {
50-
router.push(url);
51+
setUrl(url);
5152
} else {
5253
toast.error('This is not a valid code! Please try again.');
5354
}
@@ -56,14 +57,21 @@ const SignInOTP = ({email}) => {
5657
}
5758

5859
setDisabled(false);
59-
};
60+
}, []);
61+
62+
useEffect(() => {
63+
if (url) {
64+
setUrl(null);
65+
router.push(url);
66+
}
67+
}, [url]);
6068

6169
return (
6270
<InputOTP
6371
maxLength={6}
6472
value={otp}
6573
onChange={setOtp}
66-
onComplete={onSubmit}
74+
onComplete={(data) => onSubmit(data, email)}
6775
disabled={disabled}
6876
containerClassName='flex-col sm:flex-row justify-center'
6977
>
@@ -116,7 +124,7 @@ const SignInSendAgain = ({ email, onClick }) => {
116124
<CardContent className='flex flex-col gap-4'>
117125
<Divider text='or' />
118126
<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>
120128
<SignInOTP email={email} />
121129
</div>
122130
</CardContent>

0 commit comments

Comments
 (0)