Backend Boilerplates
typescript
Intermediate
Axios Interceptor with Token Refresh
Axios instance with auth token injection and automatic retry on 401.
typescript
import axios from 'axios';
const api = axios.create({ baseURL: process.env.NEXT_PUBLIC_API_URL });
api.interceptors.request.use(cfg => {
cfg.headers.Authorization = 'Bearer ' + localStorage.getItem('token');
return cfg;
});
api.interceptors.response.use(null, async err => {
if (err.response && err.response.status === 401) {
await refreshToken();
return api(err.config);
}
return Promise.reject(err);
});
export default api;
Prerequisites
- axios >= 1
Pro Tips
- → Use a refresh token mutex to prevent parallel refresh calls.
Commercial Licensing
Acquire a production access token or template license for enterprise deployment.
Module Info
- Language
- typescript
- Level
- Intermediate
- Read time
- 3 min
- Published
- Sep 2024
- License
- Commercial License
Newsletter
New modules every week, directly to your inbox.
Similar Modules
Laravel Artisan Seeder Command
Artisan command to re-seed production data with a confirmation prompt.
php
·
Intermediate
CSV to PostgreSQL Bulk Loader
Stream a large CSV directly into PostgreSQL using COPY for maximum throughput.
python
·
Intermediate
Go Event Sourcing Store
Minimal append-only event store in Go backed by a Postgres table.
go
·
Advanced