Engineering · September 24, 2026
Session auth in Go without JWT
Every tutorial reaches for JWT. We store a random token, hashed, in a table — and can log anyone out with one DELETE.
The problem with the default
A JWT is a promise you cannot take back. Once signed, it is valid until it expires, wherever it is. Revoking one means keeping a blocklist — at which point you have a session table with extra steps and a worse name.
What we do
On login: verify the password with argon2id, generate a random UUID, store SHA-256(token) in a sessions table with the user id and an expiry, set the raw token in an HttpOnly, SameSite=Strict cookie. On every request: hash the cookie, look it up, join the user. On logout: delete the row.
Why the hash
If the database leaks, the attacker gets hashes, not cookies. They cannot forge a session from a dump. The cost is one SHA-256 per request, which is nothing.
The two errors that must stay apart
pgxscan.NotFound means the session is gone: 401, clear the cookie. Anything else means the database hiccupped: 500, leave the cookie alone. Never sign someone out over a connection blip — they will not know why, and they will not trust the site again.
The Origin check
SameSite=Strict covers most CSRF, but on every non-GET we also compare the Origin header to our own host. It is three lines and it closes the door on the browsers that get SameSite wrong.
What breaks the other way
JWT with a 15-minute expiry and refresh tokens: now you have two tokens, a rotation dance, and still no way to log out a stolen refresh token without — a table.