Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Prevent duplicate OTP verification requests on signup (#4059)

Pedro Ladeira2a4879457802
diff --git a/apps/web/ui/auth/register/verify-email-form.tsx b/apps/web/ui/auth/register/verify-email-form.tsx
index 24268e7b3e..0f5d9839e5 100644
--- a/apps/web/ui/auth/register/verify-email-form.tsx
+++ b/apps/web/ui/auth/register/verify-email-form.tsx
@@ -8,7 +8,7 @@ import { OTPInput } from "input-otp";
import { signIn } from "next-auth/react";
import { useAction } from "next-safe-action/hooks";
import { useRouter, useSearchParams } from "next/navigation";
-import { useState } from "react";
+import { useCallback, useRef, useState } from "react";
import { toast } from "sonner";
import { useRegisterContext } from "./context";
import { ResendOtp } from "./resend-otp";
@@ -21,6 +21,7 @@ export const VerifyEmailForm = () => {
const { email, password } = useRegisterContext();
const [isInvalidCode, setIsInvalidCode] = useState(false);
const [isRedirecting, setIsRedirecting] = useState(false);
+ const isSubmittingRef = useRef(false);
const { executeAsync, isPending } = useAction(createUserAccountAction, {
async onSuccess() {
@@ -44,18 +45,35 @@ export const VerifyEmailForm = () => {
`/onboarding${next ? `?next=${encodeURIComponent(next)}` : ""}`,
);
} else {
+ isSubmittingRef.current = false;
+ setIsRedirecting(false);
toast.error(
"Failed to sign in with credentials. Please try again or contact support.",
);
}
},
onError({ error }) {
+ isSubmittingRef.current = false;
toast.error(error.serverError);
setCode("");
setIsInvalidCode(true);
},
});
+ const submitVerification = useCallback(() => {
+ if (
+ isSubmittingRef.current ||
+ isPending ||
+ isRedirecting ||
+ code.length < 6
+ ) {
+ return;
+ }
+
+ isSubmittingRef.current = true;
+ executeAsync({ email, password, code });
+ }, [code, email, password, executeAsync, isPending, isRedirecting]);
+
if (!email || !password) {
router.push("/register");
return;
@@ -66,7 +84,7 @@ export const VerifyEmailForm = () => {
<form
onSubmit={(e) => {
e.preventDefault();
- executeAsync({ email, password, code });
+ submitVerification();
}}
>
<div>
@@ -101,9 +119,7 @@ export const VerifyEmailForm = () => {
))}
</div>
)}
- onComplete={() => {
- executeAsync({ email, password, code });
- }}
+ onComplete={submitVerification}
/>
<AnimatedSizeContainer height>
{isInvalidCode && (