JWT Token, Refresh, Risk-Based Authentication (RBA), and OpenID Connect (OIDC) Best Practices
A opinionated practical guide built on professional experience to building session authentication correctly, with worked examples in Node.js, React, and axios.
Author: Philip Senger
The patterns and code in this guide are drawn from deployed production grade Node.js and React authentication systems designed and written by the author many times. Identifiers have been genericized so the material is portable to any project.
Who This Is For
You already know JavaScript. You've probably watched a few YouTube videos on JWTs, maybe even pasted one into jwt.io to see what's inside. But there's a real gap between an auth system that works and one that would hold up under an OWASP audit or bank-grade scrutiny. This document is for you if you've never built an auth system from scratch, or if you have and you're not fully confident it's right. Let's walk through the concepts that make the difference.
By the end, you'll understand what each piece does, why it exists, and what breaks if you get it wrong. This document builds step by step, each section depends on the one before it, so it's worth reading in order rather than jumping around. Every code sample here is safe to copy into production.
Table of Contents
- Why Tokens At All
- Anatomy of an Access Token
- Verifying a Token: The Checklist
- Where to Put the Token
- Refresh Tokens
- The Client Side: Axios Interceptors
- Risk-Based Authentication
- How To: Practical Recipes
- Common Mistakes
- Beyond One Service: OIDC, Discovery, and Distributed Security
- Standards and References
- Glossary
1. Why Tokens At All
The problem
HTTP is stateless. Every request arrives with no memory of the last one. If a user logs in on request #1, request #2 has no idea who they are. Something has to carry identity forward.
The old way: server-side sessions
The classic answer is a session table:
- User logs in. Server creates a row:
session_id → user_id.
- Server sends back
session_id in a cookie.
- Every later request sends the cookie. Server looks up the row.
This works and it is not wrong. It has one real cost: every request needs a database lookup, and every server needs to reach the same session store. Scale to twenty API instances and that store becomes a bottleneck and a single point of failure.
The token way
A token is a self-describing credential. Instead of a meaningless id that points at a row, the token itself says "this is user 42, issued at 10:00, expires at 10:05", and it carries a cryptographic signature proving the server wrote it.
The server can verify a token with nothing but a secret key. No database. No shared store. Any instance can verify any token.
The catch, and it is a big one
You cannot un-issue a token.
A session row can be deleted; the next request fails instantly. A signed token is valid until it expires, because verification is pure math over the token's own bytes. There is no "check if still valid" step unless you add one, and adding one puts the database lookup right back.
This single tradeoff drives every design decision in this document. Everything that follows is a way of managing the consequences of tokens you cannot revoke.
The standard answer, which the rest of this guide builds out: