Authenticated API Calls with Axios Interceptors
Request interceptor for Authorization header, response interceptor for 401 → refresh → retry.
The Interceptor Pattern
Rather than attaching auth headers and handling 401s in every call, Axios interceptors centralize this logic. A request interceptor adds the token; a response interceptor handles expiry and refresh transparently.
Creating the Axios Instance
Create a dedicated instance with the API base URL and withCredentials so the httpOnly refresh cookie is sent to the refresh endpoint.
import axios from 'axios'
const api = axios.create({
baseURL: '/api',
withCredentials: true // send cookies
})
export default apiAll lessons in this course
- Fastify Backend for Vue SPAs
- JWT Authentication: Login and Refresh
- Authenticated API Calls with Axios Interceptors
- Deploying Full-Stack Vue to Production