A signed token proves who a request comes from. It says nothing about whether that account is still welcome. On a platform that pays out cash, an admin blocking an account has to take effect at the next request. Two mechanisms on Redis handle that: a status cache read on every request, and refresh tokens that rotate on use.
The problem with claims
Both clients send a JSON Web Token as a Bearer header. The token carries the user’s id, type and role, and the server can verify it without a database.
Whatever the token says was true when it was signed. An admin blocks an account for abuse, and the token in that user’s app is still valid until it expires.
Loading the user from MongoDB on every request would close that gap. It would also make “find user by id” the most frequent query in the system, on the same database that carries the point transactions.
The status cache

The server keeps one small hash per user in Redis: whether the account is blocked, whether it has withdrawn, whether login is allowed, its user type and its role. A request is checked in this order.
- Verify the token’s signature and expiry.
- Reject tokens issued for a different purpose. A short-lived token that only authorises a top-up page cannot be used as a session.
- Read the status hash. On a hit, refuse the request if the account is blocked or login is switched off.
- On a miss, load the user from MongoDB, write the hash with a one-hour lifetime, and apply the same test.
The cache stays current because the admin and session paths below delete or rewrite the entry.
| Event | What happens to the keys |
|---|---|
| An admin blocks an account | The status entry and the refresh token are deleted |
| An account is converted to an advertiser | The status entry and the refresh token are deleted |
| An admin changes whether login is allowed | The status entry is rewritten with the new value |
| The user logs out | The status entry, the refresh token and the grace set are deleted |
A change made on any other path is seen when the entry expires, at most an hour later.
Entries written before the role field existed are treated as a miss, so a deploy that adds a field does not have to flush the cache. The admin checks read more of the hash: the user type has to be admin, and the blocked, withdrawn and login-allowed flags all have to be clear, before the admin’s user document is loaded from MongoDB for the route.
Refresh tokens that rotate
A refresh token obtains the next access token. Each refresh returns a new access token and a new refresh token, and the hash stored for the user is replaced, so Redis holds one current refresh token per user.

Redis holds four keys per user for this. The shapes below are the ones in use.
user:status:{id} hash blocked, withdrawn, loginAllowed, userType, role 1 hour
refresh:{id} string SHA-256 of the current refresh token 7 days
refresh:grace:{id} set hashes of tokens replaced in the last 120 seconds 120 seconds
refresh_lock:{id} lock one refresh in progress per user 10 seconds
Only a hash of the refresh token is stored, so a copy of Redis does not contain usable tokens. Refresh tokens are signed with a different secret from access tokens, and an API test asserts that an access token presented at the refresh route is rejected.
The refresh route does this:
- Verify the refresh token’s signature.
- Take the per-user lock. If another refresh holds it, wait one second and try once more. If the lock is still held, answer 429 with “refresh in progress”.
- Hash the presented token and compare it with the stored hash, then with the grace set.
- A token that differs from the stored hash and is not in the grace set gets a 401, and the user’s refresh keys are deleted.
- On a match, read the account’s status from the cache, or from MongoDB on a miss. A blocked account, or one with login switched off, gets a 403.
- Issue a new pair that carries the account’s current type and role, store the new hash, and move the previous hash into the grace set for 120 seconds.
Step 6 takes the type and role from the status read in step 5 and does not copy them from the old token. A refresh after a conversion therefore returns tokens with the new type.
Why there is a grace window
Concurrent refreshes are routine. A browser can have two tabs open. An app can fire several requests at once when it comes back to the foreground. If the access token has expired, each of them tries to refresh with the same refresh token. The first one rotates it. The others arrive milliseconds later holding a token that no longer matches the stored hash. Without a tolerance, ordinary users would be signed out by their own second tab.
Two things prevent that. The lock makes the refreshes of one user take turns. The grace set makes the later ones succeed: for 120 seconds after a rotation the previous hash is still accepted. The grace set came from a design note written in August 2026 for exactly this race.
The clients are written to make the race rare in the first place. The web client refreshes about five minutes before the access token expires, before any request has failed. The app’s HTTP client sends one refresh when it sees a 401, parks every other request in a queue until that refresh returns, and replays them with the new token.
Failure modes
| What happens | What the system does | Covered by |
|---|---|---|
| A blocked account sends a request with a valid token | The status check refuses it. Blocking also deletes its refresh token, and a refresh attempt fails the same status check with a 403 | The status check inside the shared request helper |
| Two refreshes arrive together | One takes the lock and rotates. The other waits, or is accepted inside the grace window | The rotation and grace groups of the Redis integration tests |
| A token that differs from the stored hash arrives after the window | That request gets a 401 and the stored hashes are deleted | The same integration tests |
| An access token is sent to the refresh route | Rejected, because the secrets differ | An API test |
| A refresh succeeds for an account whose role changed | The new pair carries the current type and role | A test on the claims of refreshed tokens |
| Redis is switched off by configuration | No client is created, and the status check reads the user from MongoDB on each request | A test that asserts no client is created when Redis is disabled |
The Redis tests need a real server, so they are excluded from the unit run. In CI they ran as their own job with a Redis service container.
Limits
- One refresh token per user, not per device. Signing in on a second device replaces the first device’s token. Per-device sessions would need one hash per device and a way to list them.
- Deleting a refresh token does not cancel an access token already issued. That token stays valid until it expires. The status check on each request is what stops a blocked account in the meantime.
- The status entry is trusted for up to an hour. A change that does not go through one of the four paths in the table reaches the request check only when the entry expires.
