September 7, 2026
Access Token & Refresh Token: How Web Authentication Actually Works
A complete beginner-friendly breakdown of why modern web apps rely on two tokens instead of one, how they protect against attacks, and whatβ¦
By Mayur Nikumbe
5 min read
A complete beginner-friendly breakdown of why modern web apps rely on two tokens instead of one, how they protect against attacks, and what happens behind the scenes.
Modern Identity and Access Verification Pipeline. Source: Whale Design / Getty Images
Authentication essentially means knowing exactly which user is knocking on your server's door.
Consider a simple banking application. When an API endpoint receives a request to withdraw $500 and send it to another account, the server easily understands the amount and the recipient. But it faces a critical question: Whose account balance should be deducted?
Verifying and confirming that identity on every single transaction is what authentication is all about.
How Does the Server Know Who You Are?
It all starts when a user registers on a platform:
-
You provide identifying credentials (like an email and password).
-
The server stores your profile securely in the database.
-
The server generates a unique digital pass β a Token β tied to your identity and hands it back to your device.
Think of it like getting admitted into a university or school. On day one, you fill out admission forms and receive a student ID card with your photo and roll number. When you return tomorrow, the campus security guard doesn't call the admissions office; they simply inspect your ID card to confirm you belong there.
A digital token functions the exact same way for web servers.
The Big Flaw of Relying on a Single Token
What happens if you only issue one permanent token to a user?
Protecting digital credentials from session theft. Source: Daniel Chetroni / Getty Images
Suppose User 1 logs in and receives a permanent token. If an attacker (User 2) intercepts or steals that token:
- User 2 sends requests to the server presenting User 1's token.
- The server inspects the token, reads User 1's ID, and assumes User 1 made the call.
- The attacker now has unrestricted, perpetual access to User 1's private data.
The First Fix: Token Expiration
To mitigate this vulnerability, we assign an expiration window to the token (for example, 15 minutes).
If an attacker intercepts the token, their window of opportunity closes after 15 minutes. Once the clock runs out, the server rejects any further attempts.
The catch: Does this mean our legitimate user has to re-type their email and password every 15 minutes? Absolutely not. That would ruin user experience.
This is where the two-token architecture steps in.
The Solution: Access Token + Refresh Token
Instead of one token doing all the work, we split the job between two specialized tokens:
- Access Token: The everyday pass. The user presents it to access protected data. It lives for a very short duration (e.g., 10β15 minutes).
- Refresh Token: The renewal pass. Its sole responsibility is to request a fresh Access Token whenever the current one expires. It lasts much longer (e.g., 7 to 30 days) and stays tucked away securely.
How the Silent Renewal Works
Every 15 minutes, your Access Token quietly expires.
When your frontend makes the next API request, the server returns an expiration error. Behind the scenes β without refreshing the browser or interrupting what you're doing β your client application sends the Refresh Token to an authentication endpoint.
The server validates it, issues a brand-new 15-minute Access Token, and repeats the original request. The user experiences zero friction.
Access Token vs. Refresh Token at a Glance
An Access Token and a Refresh Token serve two distinct yet complementary roles in modern authentication:
- Main Job & Purpose: The Access Token acts as your active digital keycard, unlocking private API data, routes, and protected resources with every request. In contrast, the Refresh Token never directly touches your private resources; its sole responsibility is to request a fresh Access Token whenever the current one expires.
- Lifespan: Access Tokens are deliberately short-lived, typically expiring within 5 to 15 minutes to minimize damage if intercepted. Refresh Tokens are designed for long-term session persistence, lasting anywhere from 7 to 30 days so the user doesn't have to log in repeatedly.
- Network Exposure: Because an Access Token authorizes routine operations, it experiences high network exposure, traveling in the header of almost every API call. A Refresh Token has very low network exposure, staying quietly in secure storage until it is sent to the authorization endpoint during a token renewal.
- Server State & Verification: Access Tokens are stateless, meaning the backend verifies them instantly on the fly using cryptographic signatures without running a database lookup. Refresh Tokens are stateful; they are stored, tracked, and validated against a database or cache (such as Redis) before any renewal is approved.
- Revocation & Invalidation: Revoking an Access Token before its expiration timestamp is difficult because the token is self-contained. A Refresh Token, however, can be revoked instantly by simply deleting or blacklisting the session record on the server, immediately locking out unauthorized users.
Dealing With Two Types of Attackers
What happens when bad actors target these tokens?
1. The Attacker Steals the Access Token
Because the Access Token expires within 15 minutes, the attacker's window is strictly capped. Once it expires, the attacker cannot renew it because they do not have the Refresh Token. The breach naturally terminates.
2. The Attacker Steals the Refresh Token
If an attacker manages to steal a Refresh Token, they could theoretically mint fresh Access Tokens. To defend against this, modern backends use Refresh Token Rotation (RTR).
Continuous rotation replaces old tokens on every renewal. Source: Blankstock / Getty Images
Whenever a Refresh Token is used to generate a new Access Token, the server invalidates that old Refresh Token and issues a new one alongside the new Access Token.
If the attacker uses the stolen Refresh Token first:
- The server updates the record in the database with the new token.
- When the real user's device tries to renew using their previous Refresh Token, the server detects that the token submitted has already been consumed.
- The server immediately recognizes a replay attack, invalidates the entire session family, and locks out both parties, forcing a fresh login.
Why Can't Someone Just Fake a Token?
Most Access Tokens are built using JWT (JSON Web Tokens). While anyone can decode a JWT to view its contents, nobody can forge one without the server's private secret key.
During creation, the server signs the token using a secret key only known to the backend. When a token arrives, the server verifies that signature mathematically:
- If an attacker alters the user ID inside the payload, the signature breaks.
- If an attacker tries to sign the payload using their own random key, the server's verification check fails and denies access.
Secure Storage: Protecting Tokens in Production
Where your application saves these tokens makes all the difference:
- HttpOnly Cookies for Refresh Tokens: Never store sensitive refresh tokens in standard browser storage where JavaScript can read them. Using
HttpOnlyflags guarantees that client-side scripts (and cross-site scripting attacks) cannot inspect or extract the token. - Database Hashing: Refresh tokens should never sit in plain text inside your database tables. Just like user passwords, they are hashed before storage. If a database backup is ever compromised, attackers cannot extract functional raw tokens.
- Device & Session Fingerprinting: Modern servers monitor metadata like IP regions and user-agent details. If a token suddenly requests resources from an unfamiliar browser fingerprint miles away, the system flags the anomaly and prompts re-authentication.
Final Thoughts
Relying on a single login token forces a choice between terrible security or annoying UX.
By pairing short-lived Access Tokens with monitored, rotating Refresh Tokens, web applications achieve the best of both worlds: robust protection against long-term hijacking while letting legitimate users stay logged in without repeated interruptions.