Skip to content
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
35 changes: 24 additions & 11 deletions packages/thirdweb/src/react/web/wallets/shared/OTPLoginUI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
| "valid"
| "idle"
| "payment_required";
type AccountStatus = "sending" | "sent" | "error";
type AccountStatus =
| { type: "sending" }
| { type: "sent" }
| { type: "error"; message: string | undefined };
type ScreenToShow = "base" | "enter-password-or-recovery-code";

/**
Expand All @@ -52,7 +55,9 @@
const [otpInput, setOtpInput] = useState("");
const [verifyStatus, setVerifyStatus] = useState<VerificationStatus>("idle");
const [error, setError] = useState<string | undefined>();
const [accountStatus, setAccountStatus] = useState<AccountStatus>("sending");
const [accountStatus, setAccountStatus] = useState<AccountStatus>({
type: "sending",
});

Check warning on line 60 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#L58-L60

Added lines #L58 - L60 were not covered by tests
const [countdown, setCountdown] = useState(0);
const ecosystem = isEcosystemWallet(wallet)
? {
Expand All @@ -66,7 +71,7 @@
const sendEmailOrSms = useCallback(async () => {
setOtpInput("");
setVerifyStatus("idle");
setAccountStatus("sending");
setAccountStatus({ type: "sending" });

Check warning on line 74 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#L74

Added line #L74 was not covered by tests

try {
if ("email" in userInfo) {
Expand All @@ -76,7 +81,7 @@
email: userInfo.email,
strategy: "email",
});
setAccountStatus("sent");
setAccountStatus({ type: "sent" });

Check warning on line 84 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#L84

Added line #L84 was not covered by tests
setCountdown(60); // Start 60-second countdown
} else if ("phone" in userInfo) {
await preAuthenticate({
Expand All @@ -85,15 +90,18 @@
phoneNumber: userInfo.phone,
strategy: "phone",
});
setAccountStatus("sent");
setAccountStatus({ type: "sent" });

Check warning on line 93 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#L93

Added line #L93 was not covered by tests
setCountdown(60); // Start 60-second countdown
} else {
throw new Error("Invalid userInfo");
}
} catch (e) {
console.error(e);
setVerifyStatus("idle");
setAccountStatus("error");
setAccountStatus({
type: "error",
message: e instanceof Error ? e.message : undefined,
});

Check warning on line 104 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#L101-L104

Added lines #L101 - L104 were not covered by tests
}
}, [props.client, userInfo, ecosystem]);

Expand Down Expand Up @@ -317,19 +325,24 @@

{!isWideModal && <Line />}

<Container gap="xs" p={isWideModal ? undefined : "lg"}>
{accountStatus === "error" && (
<Container
gap="sm"
p={isWideModal ? undefined : "lg"}
flex="column"

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#L328-L331

Added lines #L328 - L331 were not covered by tests
>
{accountStatus.type === "error" && (

Check warning on line 333 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#L333

Added line #L333 was not covered by tests
<Text
center
color="danger"
size="sm"
className="tw-screen-error"
>
{locale.emailLoginScreen.failedToSendCode}
{accountStatus.message ||
locale.emailLoginScreen.failedToSendCode}

Check warning on line 341 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#L340-L341

Added lines #L340 - L341 were not covered by tests
</Text>
)}

{accountStatus === "sending" && (
{accountStatus.type === "sending" && (

Check warning on line 345 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#L345

Added line #L345 was not covered by tests
<Container
center="both"
flex="row"
Expand All @@ -343,7 +356,7 @@
</Container>
)}

{accountStatus !== "sending" && (
{accountStatus.type !== "sending" && (

Check warning on line 359 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#L359

Added line #L359 was not covered by tests
<LinkButton
onClick={countdown === 0 ? sendEmailOrSms : undefined}
style={{
Expand Down
12 changes: 11 additions & 1 deletion packages/thirdweb/src/wallets/in-app/web/lib/auth/otp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,17 @@
});

if (!response.ok) {
throw new Error("Failed to send verification code");
const raw = await response.text();
let message: string | undefined;
try {
const parsed = JSON.parse(raw);
if (parsed && typeof parsed.message === "string") {
message = parsed.message;
}
} catch {

Check warning on line 61 in packages/thirdweb/src/wallets/in-app/web/lib/auth/otp.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/in-app/web/lib/auth/otp.ts#L54-L61

Added lines #L54 - L61 were not covered by tests
// ignore parse errors
}
throw new Error(message || "Failed to send verification code");

Check warning on line 64 in packages/thirdweb/src/wallets/in-app/web/lib/auth/otp.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/in-app/web/lib/auth/otp.ts#L63-L64

Added lines #L63 - L64 were not covered by tests
}

return await response.json();
Expand Down
Loading