So I was vibe coding a React frontend over the weekend(yeah, backend dev trying frontend things π ), and stumbled across something calledΒ axios response interceptors.
This frontend pattern made me go βwait, THATβs how they handle this?!β
The Problem I Never Thought About
From the backend, I send a 401 when tokens expire. Simple, right? But I never really thought about what happens on the frontend when users are mid-actionβ¦
Imagine: User spends 10 minutes filling a form, hits submit, token expired 2 minutes ago β 401 β user sees error β has to re-login βΒ loses all their work. Brutal.
The Frontend Magic I Discovered
Response interceptors essentially create this flow:
API call β 401 response β Interceptor catches it β Auto-refreshes token β Retries original call β User gets response
The user literally never knows their token expired. Pretty amazing haβ¦
Similar to response interceptor, axios has request interceptor too. No need repeat the code for sending access token or any other headers with every api call you are making in the codebase. Just write once i.e a request interceptor.
Why This Impressed My Backend Brain
- Centralized error handlingΒ (like middleware)
- Automatic retry logic
- Transparent to business logicΒ (clean separation of concerns)
- Works across the entire appΒ (write it once, forget it)
This is why I love stepping outside my backend comfort zone occasionally. You discover patterns and solutions you'd never encounter otherwise. Makes me appreciate the frontend folks even more! π
Top comments (0)