Sessions, not JWTs, for a small internal app
JWTs are the default answer to "how do I log users in". For ICS Stock, an internal inventory app for a dental clinic, I chose plain server-side sessions instead. The deciding question was simple.
When an admin deactivates a user, when should that user lose access?
The answer is "now". A stateless JWT stays valid until it expires, unless you add a deny-list, and a deny-list means you are storing server-side state anyway. Here, the state is the point.
The token
The cookie holds an opaque random token. It carries no claims and it means nothing on its own. The database stores only the SHA-256 of the token, never the token itself.
That means a database leak, from a backup or a read-only SQL injection, doesn't hand out live sessions. The attacker holds hashes of high-entropy random values and cannot reverse them. Because the tokens are random and long, a fast hash is enough here, unlike passwords, where you need a slow one.
Expiry: sliding under a ceiling
Two clocks run on every session:
- Idle expiry of 4 hours, sliding: each use pushes it forward, so someone working all morning is never logged out.
- Absolute expiry of 8 hours, fixed at login: however active you are, a session ends at the end of a working day.
The second rule is the one people forget. Without it a busy tab stays logged in indefinitely, and a stolen cookie has no natural end.
Abandoned sessions, from browsers that never come back, would pile up, so a cleanup removes them. Expired rows are dead weight but also harmless, because resolving a session always checks both clocks.
Revoking on purpose
Access must end immediately in three situations: deactivating a user, resetting a password, and changing a password. In each, every session for that user is deleted.
I don't trust a single delete to be the whole story. A failure between "password changed" and "sessions
removed" would leave old sessions alive. So the user also has a passwordChangedAt timestamp, and resolving
a session rejects any session created before it. If the delete fails, the check still holds.
Whose clock?
Session timestamps come from the application's clock, not the database's now(). I made this choice so there is a
single place that defines time. In tests I can move that clock forward and prove that a session expires after
4 idle hours or at the 8-hour ceiling, without sleeping or reaching into the database.
Resolving a session
The logic is short enough to read in one go:
async resolve(token: string, now: Date) {
const session = await this.sessions.findByHash(sha256(token));
if (!session) return null;
const idleOk = now < addHours(session.lastSeenAt, 4);
const absoluteOk = now < addHours(session.createdAt, 8);
const afterPasswordChange = session.createdAt >= session.user.passwordChangedAt;
if (!idleOk || !absoluteOk || !afterPasswordChange || !session.user.active) return null;
await this.sessions.touch(session.id, now);
return session.user;
}
Every rule in this post is one line of that function, and each has a test.
When I would pick JWTs
For several services, a mobile client, or third parties verifying tokens without calling you, stateless tokens earn their complexity. This was one API, one web app and a few dozen users. A database lookup on each request is invisible at that scale, and immediate revocation is worth much more than the saved query.
Choose the tool that matches the question you are really asking.