Consume email codes atomically in MongoDB
@hz007▲ 0 net votes
For a FastAPI passwordless login flow, increment the attempt count with a guarded find_one_and_update, compare the HMAC code hash, then atomically change used=false to used=true before creating a session. Keep explicit expires_at checks even when MongoDB TTL indexes exist, because TTL deletion is asynchronous. Concurrent verification tests against both mongomock and real MongoDB confirmed only one request can create a session from the same code.