Email verification looks like a small form: enter an address, click a button, wait for a code. In a real product, it is a distributed workflow with several clocks. The browser has one state, the API has another, and the inbox may be delayed.
That is why a verification screen can look successful while the server still has no confirmed email. It look fast, but it creates support tickets later.
This article shows a React and TypeScript approach for making the UI reflect server-owned state. The same model works whether your test fixture is a free temporary email inbox or a production mail provider.
Why email verification UIs lie
The usual implementation sets verified = true when the user submits a code. That boolean hides important questions:
- Was the code accepted, or did the request merely start?
- Is a second click creating another verification request?
- Did the request time out after the server accepted it?
- Is the displayed inbox message newer than the current verification attempt?
When these questions are not represented in state, the UI guesses. A spinner disappears, a green check appears, and the user believes the job is finished. The user dont need a prettier animation here; they need a reliable answer.
Model the flow as server-owned state
Start with a small state machine instead of a collection of booleans. For example, an email verification session can be:
type VerificationState =
| { status: "idle" }
| { status: "requesting"; email: string }
| { status: "waiting"; email: string; attemptId: string }
| { status: "confirming"; email: string; attemptId: string }
| { status: "verified"; email: string }
| { status: "error"; message: string; retryable: boolean };
The important part is not the exact names. It is that verified is only reachable after a successful server response. A local click must never be treated as proof.
Give every request an attemptId from the API. When a slow response arrives, compare its attempt ID with the active one before updating the screen. This prevents an older request from overwriting a newer attempt. If the endpoint supports it, use idempotent verification requests so refreshes and safe retries do not create confusing duplicates.
A typed React implementation
Keep the network boundary in one function and let the component dispatch explicit transitions:
type StartResponse = { attemptId: string };
async function startVerification(email: string): Promise<StartResponse> {
const response = await fetch("/api/email-verification", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ email }),
});
if (!response.ok) {
throw new Error("Could not start verification");
}
return response.json() as Promise<StartResponse>;
}
Then make the submit handler own only the transition it can prove:
const submitEmail = async (email: string) => {
setState({ status: "requesting", email });
try {
const { attemptId } = await startVerification(email);
setState({ status: "waiting", email, attemptId });
} catch {
setState({
status: "error",
message: "We could not start verification. Try again.",
retryable: true,
});
}
};
The button should be disabled only while the current request is in flight. In waiting, show the code field and a resend action with a clear cooldown. In confirming, keep the code field visible but prevent duplicate submissions. These states is slightly more work than isLoading, though the behavior is much easier to reason about.
For accessibility, put status text in an aria-live="polite" region. Do not announce every polling tick. Announce meaningful changes such as “A new code was requested” or “The code was accepted.” A disabled button should also have nearby text explaining why it is disabled; otherwise the experience feels broken.
Keep retries and test inboxes honest
Retries should be tied to the active attempt, not to a timer that runs forever. Stop polling when the session is verified, expired, or cancelled. Add a maximum wait window and show a useful recovery action when it ends.
Test the unhappy paths first:
- The first request times out in the browser.
- The server accepts the request, but the response is lost.
- A stale code arrives after a resend.
- The inbox returns an older message.
- The user clicks resend twice.
Use a dedicated test inbox or a throwaway email only for data that is safe to discard. A temp mailid in a fixture name or a dummy e mail value can be useful for testing malformed input, but those strings should never become production examples or analytics labels.
It helps to keep a failure ledger for CI: record the attempt ID, request stage, and final server response, while redacting message bodies and addresses. The failure ledger for CI email tests is a useful pattern when intermittent inbox failures start looking random.
A practical checklist
Before shipping the flow, check these details:
-
verifiedcan only come from a confirmed API response. - Every resend has an attempt ID or equivalent correlation key.
- Old responses cannot overwrite the active attempt.
- Retry and polling have explicit limits.
- Error messages explain the next action.
- Code input and status changes are keyboard accessible.
- Tests cover lost responses, stale messages, and double clicks.
- Logs contain enough correlation data to debug without exposing inbox contents.
The best verification UI is not the one with the fastest green check. It is the one that tells the truth at every step. Once the state model is server-owned, React becomes a clear projection of the workflow instead of a second, unreliable source of truth.
Top comments (0)