Email verification looks like a small form feature: collect an address, send a code, wait for a message, and let the user continue. In a real product, it is an asynchronous workflow with several ways to get stuck.
The UI are not just waiting for a promise. It is waiting for a message that may arrive late, may be unreadable, or may belong to an older attempt. A typed state machine gives the React layer a small, explicit contract for those situations.
Why email verification needs explicit UI states
The usual implementation starts with three booleans: isSending, isWaiting, and hasError. That works until a user clicks resend during polling, changes the address, or opens a verification link in another tab. Then combinations appear that the product never intended to show, such as a success message beside an expired-code error.
Instead, model the meaningful states:
-
idle: no address has been submitted -
sending: the request to create a verification attempt is in progress -
waiting: the attempt exists and the client is waiting for a matching message -
verified: the server accepted the code or link -
expired: the attempt lease ended -
failed: the user can retry after a recoverable error
This rules out accidental combinations. It also makes the copy, disabled buttons, and accessibility announcements easier to review together.
Model the flow as a discriminated union
TypeScript can make invalid rendering paths harder to write. Each state carries only the data that makes sense for it:
type VerificationState =
| { kind: "idle" }
| { kind: "sending"; email: string }
| { kind: "waiting"; email: string; attemptId: string; expiresAt: number }
| { kind: "verified"; email: string }
| { kind: "expired"; email: string }
| { kind: "failed"; email: string; message: string; canRetry: boolean };
The attemptId is important. It prevents a late response from an older request changing the current screen. The expiry timestamp is useful too: the client can show a countdown, but the server must still enforce the actual lease.
I prefer a reducer for transitions because each event is visible in one place. A resend action should create a new attempt, clear the old polling task, and move back through sending; it should not quietly mutate a few unrelated flags.
Keep polling and inbox access behind a boundary
React should not know how a message is found. The component can call a small API such as waitForVerification(attemptId), while the service handles bounded polling, correlation IDs, and cleanup.
For staging and automated checks, an isolated disposable email generator can help inspect the message without mixing it with a personal inbox. It is test data, not proof that a production identity is trustworthy. Give each run a unique recipient and a short expiry.
The service boundary might look like this:
async function waitForVerification(
attemptId: string,
signal: AbortSignal,
): Promise<{ code: string }> {
for (let tryNumber = 0; tryNumber < 12; tryNumber += 1) {
const response = await fetch(`/api/verification/${attemptId}`, { signal });
if (response.status === 200) return response.json();
if (response.status === 410) throw new Error("verification expired");
if (!response.ok) throw new Error("verification lookup failed");
await new Promise((resolve) => setTimeout(resolve, 1500));
}
throw new Error("verification timed out");
}
In production, the server should usually push a completion event or expose a carefully bounded endpoint. The loop is mainly useful as a simple adapter and for test environments. It need a cancellation path: when the address changes or the component unmounts, abort the old attempt.
If your workflow runs in CI, pair the adapter with an AWS deployment drift check so environment differences do not look like random email failures. For browser tests, inbox lease patterns for Cypress retries are a useful reminder that a retry needs a new, identifiable fixture.
Render useful React feedback
Keep the component focused on user decisions. In waiting, show the destination in a privacy-conscious way, offer “use a different address,” and provide a resend action only when the current attempt allows it. Do not expose the full verification token in the DOM or logs.
An aria-live="polite" region can announce “Verification email sent” and later “Code expired, request a new one.” Reserve space for these messages so the form does not jump around. A bit more clear copy usually beats a clever animation here.
People also search for phrases like temp gamil com when they are trying to find a temporary inbox. That typo can be useful in support analytics, but it should never become a label or a reason to weaken validation.
Testing the transitions
Test events and transitions, not just the final happy-path screenshot. At minimum, cover:
- A successful send moves from
idletowaiting. - A late response from an old
attemptIdis ignored. - A
410response renders the expired state and enables a new request. - Aborting the request on unmount does not show an error toast.
- A matching code moves to
verified, while an unrelated message is rejected. - Keyboard users can reach resend and change-address actions in the right order.
The test don't need a real mailbox for every transition. Inject the verification service and return deterministic results. Reserve a smaller end-to-end suite for checking that the sender, inbox adapter, and application agree on the correlation contract.
Questions developers usually ask
Should the client poll forever?
No. Set a server-enforced expiry and a client timeout. “Try again” is more honest than a spinner that never ends.
Is a disposable temporary email safe for production accounts?
No. Use isolated test fixtures only, and never place recovery credentials or private customer data in them. A disposable inbox is a development tool with a narrow purpose.
Do I need a state machine library?
Not always. A discriminated union and reducer are enough for a compact flow. A library becomes more useful when verification is part of a larger process with parallel tasks, retries, and persisted transitions.
Final takeaway
Email verification becomes easier to ship when the UI treats it as a contract instead of a collection of flags. Define the states, correlate every attempt, bound the wait, and test the transitions that users actually see. The result is more predictable React code, cleaner TypeScript types, and fewer mysterious “the email never arrived” reports.
Top comments (0)