· 3 min read

One account, three places it has to be true

How a Typestrix sign-in reaches the socket server, why a valid session can still show an old name, and why a resend countdown is not a rate limit.

Shivank Pandey

A Typestrix account lives in the Next.js app: email and password or Google, handled by better-auth. But the account has to be true in more places than the web app. The socket server needs to know who is racing. The profile has to show the name you just saved. And the emails that verify and recover the account must not become a spam cannon.

Each of those is a different problem. This post covers all three.

Getting a session onto a socket

Races run on a separate Bun process. If the browser could simply say "I am user X" in a join message, anyone could join as anyone. So the socket server only accepts an identity it can verify itself: a small token the web app signs from the real session, moments before the connection opens.

Pick a step, or use the arrows, to walk through the diagram.

Step through it. Only the last step creates a connection.

The token carries the user id plus the display fields the roster needs: name, avatar and player tag. Because they are inside the signed payload, the socket server can show names and avatars without a database query per message, and it ignores any name the browser puts in a join message.

Verification fails closed at the first problem: the token's shape, the algorithm (only HS256, never none), a constant-time signature check, then the time claims, then bounds on every display field. The signature says who produced the payload; the bounds say whether it is safe to show. Those are different questions, so both run, even though the only issuer is our own web app.

One of those time checks was added later as a fix: a token whose issue time is in the future is rejected, with a 30-second allowance, because two servers' clocks never agree exactly. Without the allowance, a server running slightly behind would reject brand-new tokens from one running slightly ahead.

The trade-offs are deliberate. A token cannot be revoked during its five minutes, and an open socket is not re-checked. The name inside it is a snapshot, so renaming yourself mid-race shows up for others when you next reconnect. For a short-lived connection credential, that is the right balance.

Valid is not the same as fresh

better-auth keeps sessions for seven days, renewed as you use the site. To avoid a storage read on every request, it also keeps a signed copy of the session in a cookie for ten minutes. That cache is why a session can be perfectly valid and still contain the name you had five minutes ago.

So freshness is handled where staleness would be visible. The profile page and the socket token both read the session uncached. And saving a profile edit refreshes the cache right away:

Pick a step, or use the arrows, to walk through the diagram.

Step through it. Leaderboard rows and open sockets keep their own snapshots.

Other surfaces hold their own copies on purpose. A leaderboard row stores the name from when that best was set, and an open race keeps the identity from its token. They catch up on the next best and the next connection.

Route protection follows the same "check where it matters" idea. A lightweight proxy redirects to sign-in when there is no session cookie at all, which keeps navigation fast. It is a convenience, not the lock: every protected page and action verifies the session on the server again.

A resend countdown is not a rate limit

Verification and password-reset emails need two things that are easy to confuse. The button needs to explain when you can try again. The server needs to decide whether an email actually goes out.

Pick a step, or use the arrows, to walk through the diagram.

Step through it. Only the last three layers run on the server.

The endpoint limit taught me the most. Counters live in Redis, and until recently the storage adapter had no atomic increment, so the auth library fell back to reading the count, adding one and writing it back. Two requests arriving together could both read the same number and both slip under the limit. The fix was a small adapter method that lets Redis increment the counter in one step, so every request gets its own number. It is the kind of bug that never shows up in testing and always shows up under load.

The per-address budgets are keyed by a hash of the normalised email, so raw addresses never appear in key names. When a budget is exhausted, the error carries the seconds left in the window. The browser stores that as an absolute deadline rather than a number that counts down, so reloading the tab does not reset the wait.

The common thread

Identity, freshness and limits all fail the same way when they are treated as one thing. A signed-in user is not necessarily a fresh profile, a cookie is not proof of a session, and a disabled button is not a limit. Giving each its own check, in the place where it matters, is most of the work.

Keep reading