Checking a login takes 120 microseconds when the public key is cached, and the cache has four rules for when Supabase is down
Every request to a signed-in part of Munchable has to answer one question: who is this? The obvious way, auth.getUser(), asks the auth server to confirm the token. The comment at the top of our session module puts the co
Every request to a signed-in part of Munchable has to answer one question: who is this? The obvious way, auth.getUser(), asks the auth server to confirm the token. The comment at the top of our session module puts the cost at 150 to 700 ms from Europe, because the auth project lives in the US, and it is paid by the request proxy and then again by every gate and API route behind it.
The alternative is to verify the token yourself. Supabase signs access tokens with an asymmetric key and publishes the public half at a well-known URL. A signature and an expiry can be checked locally, with no network at all. The client library will do that if you hand it the key set, so the module lib/auth/session-claims.ts has one job: get the key set into the library, cheaply and safely.
What the check costs
I wrote a throwaway benchmark: generate an ES256 key pair, sign one token, then call our verifiedClaims 2,000 times with the key set supplied.
{ N: 2000, totalMs: 240, perCallMicros: 120 }
About 120 microseconds per verification on my laptop. That is the whole price of knowing who someone is on every request, against a network round trip measured in hundreds of milliseconds. I did not time the round trip myself; the 150 to 700 ms figure is the one in the code comment, so treat the ratio as indicative.
The module ships with four tests, and they are the part I would copy. The Supabase client in them points at http://127.0.0.1:9, a port with nothing on it. If verification ever reached for the network the test would fail instead of passing by accident. The cases are a valid token, an anonymous session, an expired token, and a token signed by a different key that claims the same kid. That last one is the attack: it has the right label and the wrong signature, and it must come back as null.
The cache is where the decisions are
The key set is a document you fetch over the network, so "verify locally" really means "fetch once, then reuse". The interesting code is the reuse. There are four rules, and each one exists because the naive version has a failure.
1. One fetch per ten minutes, per process. A cached document with a ten minute TTL. Keys rotate rarely, so this is far more often than needed and far less often than every request.
2. Concurrent callers share one fetch. When the cache is cold, a burst of requests would each start their own fetch. A single in-flight promise is shared, and cleared in finally, so a thundering herd on a cold start becomes one request.
3. A failure is remembered for thirty seconds. If Supabase is unreachable, the fetch has a three second timeout. Without a memory of failure, every request in the next minute would wait three seconds and fail again. With it, the outage costs one slow request, and everything after it returns immediately for half a minute.
4. A failed refresh keeps the last good document. When the ten minutes are up and the refetch fails, the stale key set is still used. A key set that is ten minutes old is almost certainly still right, and a login that fails because the key server blinked is certainly wrong.
There is also a fifth case, handled in the caller rather than the cache. If a token arrives with a kid that is not in the cached set, the keys have rotated. The module drops the cache so the next call fetches the new document, instead of the library paying its own fallback fetch on every call until the TTL runs out.
What you give up
Local verification has one cost, and the comment at the top of the file states it plainly. A token stays valid until it expires, an hour at most, even if the session was revoked in the meantime, because nothing is asking the server. The module's argument for accepting that is specific to the app: nothing behind these gates spends money or writes on a revoked session's behalf in a way the hourly bound does not cover, and the destructive routes act on the caller's own account only. That is a claim about a particular product, and it is the thing to re-check before copying the pattern to a different one.
There is a quieter fallback too. If a project still signs tokens with the legacy shared secret instead of an asymmetric key, the library falls back to getUser() by itself and the speed-up silently disappears. The module logs one line in development saying which case applies, so the failure mode is "slower" rather than "broken", and you can tell which you have.
See it from the outside
None of this is visible on a page, which is rather the point. What you can see is what it protects: the part of munchable.app that works the same whether you arrive from the website or the phone app, because both present a token and the same function checks it. The about page says who builds it, and the privacy page describes the little that an account holds.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.