Course contentsShow
← Security
Lesson 1091 of 3,05222. Web Security - Client-Side and SPA SecurityFree lesson

Silent Authentication and Token Refresh

Implementing silent token renewal using hidden iframes or refresh tokens to maintain user sessions without visible redirects.

Silent Authentication and Token Refresh

What you'll learn: How to keep users logged in seamlessly without interrupting their experience or exposing long-lived tokens.

The Challenge: Balancing Security and User Experience

Access tokens (JWTs) used in SPAs should be short-lived (5-15 minutes) to limit damage if stolen. But forcing users to log in every few minutes destroys usability. How do you refresh tokens without interrupting the user?

Think of it like a movie theater: Your ticket (access token) expires after 2 hours, but you have a "season pass" (refresh token) that lets you get a new ticket at the concession stand—without going back through the front gate.

Two Main Approaches

1. Hidden Iframe Approach (Legacy)
The SPA loads a hidden iframe pointing to the authorization server with prompt=none. If the user still has a valid session (via secure session cookie), the server silently issues a new token and redirects back with it—all invisible to the user.

Limitations: Relies on third-party cookies, which modern browsers increasingly block. Works only when auth server and SPA share the same domain context.

2. Refresh Token in Secure Storage
After initial login with PKCE, store the refresh token securely (httpOnly cookie or memory with BFF pattern). Before the access token expires, your app silently exchanges the refresh token for a new access token via a backend-for-frontend (BFF) endpoint or directly with the auth server.

Implementation Pattern

Your SPA monitors token expiration and triggers refresh 1-2 minutes before expiry. This happens in the background—no user action required. Failed refresh means the session expired, so redirect to login.

Key Takeaway: Silent authentication maintains seamless user sessions by automatically refreshing short-lived access tokens using either hidden iframes with session cookies or refresh tokens, keeping security high without sacrificing usability.