Course contentsShow
← Web Development
Lesson 1607 of 2,98229. Web Authentication PatternsFree lesson

Silent Token Refresh Pattern

Implement automatic token renewal before expiration to maintain seamless user sessions without login prompts.

Silent Token Refresh Pattern

You'll learn how to automatically renew access tokens behind the scenes, keeping users logged in without interrupting their experience.

What Is Silent Token Refresh?

The Silent Token Refresh pattern automatically requests a new access token before the current one expires, using a long-lived refresh token—all without the user noticing. Think of it like automatically renewing your parking meter before it expires, so you never get a ticket or have to rush back to your car.

This pattern prevents the jarring experience of users being suddenly logged out mid-session or seeing a login prompt while actively using your application.

How It Works

When your application loads or detects that an access token is nearing expiration, it silently sends a background request (typically to a /refresh endpoint) using the refresh token stored in an HTTP-only cookie. The server validates the refresh token and issues a new access token—and possibly a new refresh token. Your app updates the stored token and continues seamlessly.

The "silent" part means this happens automatically in the background. Users keep interacting with your application without any authentication-related interruptions.

Typical Implementation Approach

You set up a timer or interval that checks token expiration time (from the exp claim you learned earlier). A few minutes before expiration, trigger the refresh request. Some implementations use HTTP interceptors that catch 401 errors and attempt a refresh before retrying the original request.

Since you're using HTTP-only cookies for refresh tokens (as covered in previous lessons), the cookie is sent automatically with the refresh request—no manual token handling needed in JavaScript, which reduces XSS risk.

Key Takeaway: The Silent Token Refresh pattern maintains uninterrupted user sessions by proactively renewing access tokens in the background before they expire, creating a seamless authentication experience.