Urgent.News

What's breaking now, across hundreds of outlets.

Tech

State Machines for Email Verification APIs

State Machines for Email Verification APIs Email verification looks like a small feature until retries, expired tokens, duplicate requests, and delayed workers enter the picture. A single verified boolean cannot explain those cases. A persisted state machine gives a REST API explicit rules, makes PostgreSQL enforce part of the contract, and keeps authentication behavior predictable. This design…

Email verification often appears as a small feature, but it becomes complex when retries, expired tokens, duplicate requests, and delayed workers are involved. A REST API cannot fully explain these cases with a simple verified boolean. Persisting state machine rules provides explicit behavior, enforces constraints on PostgreSQL, and maintains consistent authentication.

A verified flag alone cannot determine if a token was never issued, a second token was requested, the original token expired, a token was consumed, or delivery is temporarily failing. Each condition requires different responses and actions. Testing these edge cases helps prevent bugs later on.

A potential state machine consists of the following states: pending, sent, verified, expired, and blocked. The transition rules ensure that a consumed token never becomes valid again, and an expired token can only transition to the sent state after a new token is created. A blocked account should not revert to pending if a delayed worker arrives late.

Persisting legal transitions in PostgreSQL involves creating a separate table for the current status and token event. The schema stores the status as an enumerated type, and the version column serves as an optimistic concurrency check. Updating the status is an atomic operation that depends on matching the current status, expiration time, token hash, and version. This ensures that the update cannot be applied twice by concurrent requests.

To make the REST API idempotent, a new endpoint should accept an Idempotency-Key header. Upon a successful transition, the response should be recorded in the same transaction or a durable store. Repeating a request with the same key returns the recorded result, while a different body triggers a conflict. A clean contract helps manage retries rather than relying on unreliable network assumptions.

The email adapter receives an immutable event after the database commit. For asynchronous delivery, an outbox row prevents the failure where the API reports success but the queue message was never created. A delayed worker must re-check the row version before sending to avoid sending a token that the user already replaced.

When testing, cover scenarios like two confirmations with the same token, two confirmations with the same idempotency key, a token at the exact expiry boundary, a delayed send worker after a replacement token, a blocked account, and a malformed token. A simple fixture email address can help test delivery and cleanup in integration tests but should not be used to prove the production identity or authorization system's trustworthiness.

Rewritten by urgent.news; may contain errors — check the original.

Source: dev.to

Read the original at dev.to →

Report a problem with this story

More in Tech

Adding a Cut button to videos on every site in Manifest V3, without scary install permissions

I maintain CutThisX , a free web tool that cuts a part of an online video from its link and downloads only that part. Last week I shipped version 1.1 of its browser extension, whose whole job is a…

  • CutThisX adds "Cut" button to videos without install permissions
  • Extension works on video hosting sites and other websites with different permissions
  • Service worker dynamically registers content scripts based on accessed origins

TradingView Turtles Strategy: Does It Still Work?

The short version. This is a TradingView Turtles strategy backtest, a follow-up to my five-part series on the original Turtle rules.

  • TradingView Turtles Strategy based on Donchian channel breakouts
  • Strategy published on TradingView in 2015, 6,553 boosts
  • Positive results on strong upward markets, fails on flat/downward trends

Migrating my own SaaS onto a new Laravel codebase: 9 things production taught me

This week I rebuilt GraceSoft Desk, a small accounting and project tool I run my own business on, on a fresh Laravel 13 + Livewire 4 codebase.

  • Production's outdated schema required importer to adapt by checking table and column existence.
  • Missing receipts resolved by checking both recorded path and fallback path under "documents/".
  • Column storing bucket names updated from 32 to 100 characters to accommodate larger names.

More from Saturday, 28 November 2026 →