Skip to main content
100%

axios interceptors for refresh token

✓ Published0🌍 Public
AAbhay-Joshi-Git
Last edited Oct 5, 2017
Created on Aug 4, 2018

This example shows an Axios response interceptor that automatically handles expired access tokens by refreshing them when a request returns a 401 status. The code intercepts the failed request, checks the stored refresh token from `window.localStorage`, and posts it to an authentication endpoint at `localhost:8000/auth/refresh`. On success, it updates both the access and refresh tokens in local storage, sets the new authorization header for subsequent requests, and retries the original request. The implementation uses `axios.interceptors.response.use` with a `_retry` flag to prevent infinite loops, while relying on the browser’s local storage for token persistence and Axios’s promise-based API for asynchronous flow.

AI-generated description

Similar vizzes