React Email Verification: Make Pending a Real State
Email verification often starts as a tiny React feature: show a form, call an endpoint, then display “verified.” The rough edges arrive between those moments: delayed messages, expired codes, resend races, and refreshes at the wrong time.
The fix is to stop treating verification as a boolean. In a TypeScript UI, model the journey as a small state machine with explicit pending, expired, and failure states. It makes the screen easier to understand and the API contract easier to test. A free temporary email inbox or a test fixture is useful during development, but it should not hide lifecycle bugs.
The UI problem: verification is not a boolean
isVerified: boolean cannot explain what the user should do next. false might mean that the request has not started, the email is on its way, the code was rejected, or the verification link expired. Those are different product experiences.
A useful flow has at least these states:
-
idle: the user can submit an email address. -
pending: the request was accepted and the UI is waiting. -
codeRequired: a code or link is ready to be entered. -
verified: the server confirmed the identity. -
expired: the user needs a new message. -
error: the UI can explain a recoverable problem.
This distinction matters for automated checks too. A test that searches a temp mail mail inbox should not pass just because the form stopped showing a spinner. It needs a real server event and a stable state transition.
Model the flow as a discriminated union
TypeScript makes invalid combinations harder to represent. Keep the data needed by each state next to that state instead of making every field optional:
type VerificationState = | { kind: "idle" } | { kind: "pending"; requestId: string } | { kind: "codeRequired"; expiresAt: string } | { kind: "verified"; verifiedAt: string } | { kind: "expired" } | { kind: "error"; message: string; retryable: boolean }; Now a component cannot accidentally render an expiry timer when no expiry exists. The reducer can reject an old response that tries to move verified back to pending.
The request ID is a small but important boundary. When the user clicks resend, store the new ID and ignore responses belonging to an earlier request. This is the same idea as a signal boundary for Playwright email tests: each result needs evidence that it belongs to the action being observed.
Keep resend and polling behind explicit actions
Avoid putting a collection of loosely related booleans in the component. Use actions with clear intent:
type VerificationAction = | { type: "submit"; requestId: string } | { type: "messageReceived"; expiresAt: string } | { type: "resend"; requestId: string } | { type: "verify"; verifiedAt: string } | { type: "expire" } | { type: "fail"; message: string }; The resend button should have a cooldown owned by the flow, not by a random setTimeout that can survive an unmount. Disable it while a request is in flight, show when it can be used again, and cancel timers during cleanup. Forgotten timers are why the UI sometimes says “sent” after the user already left.
For polling, use a deadline and an abort signal. Stop when the message arrives, the code expires, or the deadline is reached. Do not poll forever just because the inbox is slow. In a real project, this is where a privacy review for email sandboxes belongs: test messages may contain tokens, so logs should record IDs and statuses rather than entire bodies.
Render honest feedback in React
Render one primary instruction for every state. Pending should say what is happening and what the user can do while waiting. Expired should offer a direct resend path. Error should distinguish “try again” from “contact support.” The copy doesnt need to be dramatic, it needs to be precise. Its job is clarity.
An accessible status region helps both screen readers and automated tests:
<p role="status" aria-live="polite"> {state.kind === "pending" && "Check your inbox. We are waiting for the message."} {state.kind === "expired" && "That code expired. Request a new one."} {state.kind === "verified" && "Email verified. You can continue."} </p> Do not announce success when the client only received a 202 response. The backend accepted the request, it did not prove the mailbox was read. That small wording change avoids a false success that users notice quickly.
Search terms in bug reports can be messy, including phrases such as fake e mail com. Keep those phrases out of identifiers and analytics schemas; they are search hints, not part of the domain model. Also, a little informations is better than a blank spinner, especially when delivery can be delayed. The details is what keeps users calm.
Test the uncomfortable states
The happy path is valuable, but the quality of this feature lives in the awkward paths. Test at least:
- A slow response keeps the UI in
pendingand prevents duplicate submits. - A late response from an old request cannot overwrite a newer resend.
- An expired code shows a resend action and clears the old input.
- A rejected code stays recoverable without claiming verification.
- A successful response exposes the next step to keyboard and screen-reader users.
Use fake timers for cooldowns and a controlled email fixture for delivery. The test should assert state transitions, not sleep for an arbitrary number of seconds. If the test need to wait, it should wait on a signal from the fixture or API, then fail with the request ID and current state. These checks is much easier to diagnose.
Questions developers usually ask
How much state is enough?
Start with the states that change user action: idle, pending, code required, verified, expired, and error. Add another state only when it changes behavior or explanation. More states are not automatically more robust.
Conclusion
Email verification becomes calmer when “pending” is treated as a real state instead of a gap between booleans. A typed union, request IDs, bounded polling, honest status copy, and tests for late or expired messages give React teams a compact contract to build on. The result is cleaner implementation notes, fewer false positives, and a verification screen that explains itself when delivery is not instant.
