Skip to content

Commit 350ffcd

Browse files
committed
feat: adds otp cooldown timer
1 parent 1c135b0 commit 350ffcd

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

.changeset/deep-shirts-worry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Adds countdown for email cooldown

packages/thirdweb/src/react/web/wallets/shared/OTPLoginUI.tsx

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export function OTPLoginUI(props: {
5353
const [verifyStatus, setVerifyStatus] = useState<VerificationStatus>("idle");
5454
const [error, setError] = useState<string | undefined>();
5555
const [accountStatus, setAccountStatus] = useState<AccountStatus>("sending");
56+
const [countdown, setCountdown] = useState(0);
5657
const ecosystem = isEcosystemWallet(wallet)
5758
? {
5859
id: wallet.id,
@@ -76,6 +77,7 @@ export function OTPLoginUI(props: {
7677
client: props.client,
7778
});
7879
setAccountStatus("sent");
80+
setCountdown(60); // Start 60-second countdown
7981
} else if ("phone" in userInfo) {
8082
await preAuthenticate({
8183
ecosystem,
@@ -84,6 +86,7 @@ export function OTPLoginUI(props: {
8486
client: props.client,
8587
});
8688
setAccountStatus("sent");
89+
setCountdown(60); // Start 60-second countdown
8790
} else {
8891
throw new Error("Invalid userInfo");
8992
}
@@ -184,6 +187,23 @@ export function OTPLoginUI(props: {
184187
}
185188
}, [sendEmailOrSms]);
186189

190+
// Handle countdown timer
191+
useEffect(() => {
192+
if (countdown <= 0) return;
193+
194+
const timer = setInterval(() => {
195+
setCountdown((current) => {
196+
if (current <= 1) {
197+
clearInterval(timer);
198+
return 0;
199+
}
200+
return current - 1;
201+
});
202+
}, 1000);
203+
204+
return () => clearInterval(timer);
205+
}, [countdown]);
206+
187207
if (screen === "base") {
188208
return (
189209
<Container fullHeight flex="column" animate="fadein">
@@ -298,8 +318,17 @@ export function OTPLoginUI(props: {
298318
)}
299319

300320
{accountStatus !== "sending" && (
301-
<LinkButton onClick={sendEmailOrSms} type="button">
302-
{locale.emailLoginScreen.resendCode}
321+
<LinkButton
322+
onClick={countdown === 0 ? sendEmailOrSms : undefined}
323+
type="button"
324+
style={{
325+
opacity: countdown > 0 ? 0.5 : 1,
326+
cursor: countdown > 0 ? "default" : "pointer",
327+
}}
328+
>
329+
{countdown > 0
330+
? `Resend code in ${countdown} seconds`
331+
: locale.emailLoginScreen.resendCode}
303332
</LinkButton>
304333
)}
305334
</Container>

0 commit comments

Comments
 (0)