Skip to content

Feature: Adds OTP cooldown timer #6439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/deep-shirts-worry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Adds countdown for email cooldown
33 changes: 31 additions & 2 deletions packages/thirdweb/src/react/web/wallets/shared/OTPLoginUI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
const [verifyStatus, setVerifyStatus] = useState<VerificationStatus>("idle");
const [error, setError] = useState<string | undefined>();
const [accountStatus, setAccountStatus] = useState<AccountStatus>("sending");
const [countdown, setCountdown] = useState(0);

Check warning on line 56 in packages/thirdweb/src/react/web/wallets/shared/OTPLoginUI.tsx

View check run for this annotation

Codecov / codecov/patch

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

Added line #L56 was not covered by tests
const ecosystem = isEcosystemWallet(wallet)
? {
id: wallet.id,
Expand All @@ -76,6 +77,7 @@
client: props.client,
});
setAccountStatus("sent");
setCountdown(60); // Start 60-second countdown

Check warning on line 80 in packages/thirdweb/src/react/web/wallets/shared/OTPLoginUI.tsx

View check run for this annotation

Codecov / codecov/patch

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

Added line #L80 was not covered by tests
} else if ("phone" in userInfo) {
await preAuthenticate({
ecosystem,
Expand All @@ -84,6 +86,7 @@
client: props.client,
});
setAccountStatus("sent");
setCountdown(60); // Start 60-second countdown

Check warning on line 89 in packages/thirdweb/src/react/web/wallets/shared/OTPLoginUI.tsx

View check run for this annotation

Codecov / codecov/patch

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

Added line #L89 was not covered by tests
} else {
throw new Error("Invalid userInfo");
}
Expand Down Expand Up @@ -184,6 +187,23 @@
}
}, [sendEmailOrSms]);

// Handle countdown timer
useEffect(() => {
if (countdown <= 0) return;

Check warning on line 192 in packages/thirdweb/src/react/web/wallets/shared/OTPLoginUI.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/wallets/shared/OTPLoginUI.tsx#L191-L192

Added lines #L191 - L192 were not covered by tests

const timer = setInterval(() => {
setCountdown((current) => {
if (current <= 1) {
clearInterval(timer);
return 0;
}
return current - 1;
});
}, 1000);

Check warning on line 202 in packages/thirdweb/src/react/web/wallets/shared/OTPLoginUI.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/wallets/shared/OTPLoginUI.tsx#L194-L202

Added lines #L194 - L202 were not covered by tests

return () => clearInterval(timer);
}, [countdown]);

Check warning on line 205 in packages/thirdweb/src/react/web/wallets/shared/OTPLoginUI.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/wallets/shared/OTPLoginUI.tsx#L204-L205

Added lines #L204 - L205 were not covered by tests

if (screen === "base") {
return (
<Container fullHeight flex="column" animate="fadein">
Expand Down Expand Up @@ -298,8 +318,17 @@
)}

{accountStatus !== "sending" && (
<LinkButton onClick={sendEmailOrSms} type="button">
{locale.emailLoginScreen.resendCode}
<LinkButton
onClick={countdown === 0 ? sendEmailOrSms : undefined}
type="button"
style={{
opacity: countdown > 0 ? 0.5 : 1,
cursor: countdown > 0 ? "default" : "pointer",
}}

Check warning on line 327 in packages/thirdweb/src/react/web/wallets/shared/OTPLoginUI.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/wallets/shared/OTPLoginUI.tsx#L321-L327

Added lines #L321 - L327 were not covered by tests
>
{countdown > 0
? `Resend code in ${countdown} seconds`
: locale.emailLoginScreen.resendCode}

Check warning on line 331 in packages/thirdweb/src/react/web/wallets/shared/OTPLoginUI.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/wallets/shared/OTPLoginUI.tsx#L329-L331

Added lines #L329 - L331 were not covered by tests
</LinkButton>
)}
</Container>
Expand Down