How to install JWT access token and refresh token system?
Establish a robust session refresh system with ephemeral JWT access token, rotated refresh token, reuse detection, and secure cookie settings.
JWT access token and refresh token systemUsed to authorize API requests with a short-lived access token while not forcing the user to log in again each time it expires. The basis of the secure design is a short access token duration, a refresh token session that can be monitored on the server, and token rotation at each refresh.
Using JWT alone does not make the system secure. Where the token is stored, how to understand whether the stolen refresh token is reused, and which record is canceled during logout are the main parts of the design.
Separate access token and refresh token tasks
The access token is sent to the API and carries the user ID, target audience, issuer, authorizations and a short expiration time. API uses the signature on each request,iss, aud, expand if necessarynbfmust verify the fields. Just because the token can be decoded does not mean it is valid.
Refresh token is sent only to the refresh endpoint. Since it is long-lived, it is a safer model because it can be associated with the session record in the database, hashed, stored and canceled. The refresh token does not have to be JWT; A random value with high entropy is more manageable.
Recommended session table
CREATE TABLE refresh_sessions (
id UUID PRIMARY KEY,
user_id UUID NOT NULL,
token_hash CHAR(64) NOT NULL UNIQUE,
family_id UUID NOT NULL,
expires_at TIMESTAMP NOT NULL,
used_at TIMESTAMP NULL,
revoked_at TIMESTAMP NULL,
replaced_by UUID NULL,
created_at TIMESTAMP NOT NULL
);
If the database is compromised, the token hash is kept so that plain refresh tokens cannot be used directly. The token value is only given to the client at the time it is created. The fast but cryptographic SHA-256 for hashing can be used for comparison purposes when the token is sufficiently random and long.
Apply rotation in refresh flow
- The client sends the refresh token to the refresh endpoint over HTTPS only.
- The server finds the token hash; Checks duration, cancellation and usage status.
- If valid, marks the old record as used.
- It generates a new refresh token with the new access token and opens a new record in the same family.
- If the old token comes back, possible token theft is accepted and the relevant family is canceled.
These operations should be done in a single database transaction to avoid race conditions. A short, controlled repeat policy can be designed to take into account that the same refresh request may be unintentionally sent twice due to a mobile application or poor connection; But the old token should not be reused indefinitely.
Browser storage preference
for refresh tokenHttpOnly, Secureand suitableSameSiteThe cookie set prevents JavaScript from directly accessing the token. When cookies are used, the CSRF model should be considered separately. If the SameSite setting does not match the architecture, additional protection such as CSRF token or origin control is required.
Set-Cookie: refresh_token=...; HttpOnly; Secure; SameSite=Lax; Path=/auth/refresh
Keeping the Access token in localStorage for a long time may lead to the token being read in case of XSS. Depending on the application architecture, short-term storage in memory or Backend for Frontend approach can be considered.
Signing and key management
Fix the algorithm in the server config; Do not blindly accept the algorithm requested by the token header. Asymmetric keys in distributed systems andkidControlled key rotation can be used with . Do not store secret keys in the source code or client application.
Logout and security events
During logout, cancel the refresh session on the server and delete the cookie with the same path/domain settings. In case of password change, account closure, suspicious device or refresh token reuse, you must be able to cancel the relevant sessions or all user sessions. Apply an inactivity period to unused refresh sessions.
Do not write token values in the log. Session ID, user ID, event type and time information are sufficient in the logs; Minimize personal and security sensitive data.
Short guide
Common mistake
Accepting long-lived refresh tokens without server registration and rotation; The stolen token can be used silently for its duration.