-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathbasic-refresh.ts
More file actions
38 lines (30 loc) · 1.21 KB
/
basic-refresh.ts
File metadata and controls
38 lines (30 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* Basic Refresh Example
*
* Demonstrates the core 401 → refresh → retry flow:
* 1. A request fails with 401 (invalid token)
* 2. The refresh logic runs and updates the token
* 3. The original request is retried with the new token and succeeds
*/
import axios from 'axios';
import { createAuthRefresh } from '../src/index';
import { createMockAdapter, MockState } from './_helpers/mock-adapter';
import { assertEqual } from './_helpers/assert';
const state: MockState = { validToken: 'token-v2', refreshCount: 0 };
const instance = axios.create({
adapter: createMockAdapter(state),
headers: { Authorization: 'Bearer token-v1' }, // starts with an expired token
});
createAuthRefresh(instance, async (failedRequest) => {
state.refreshCount++;
// Simulate obtaining a new token
state.validToken = 'token-v2';
failedRequest.response.config.headers['Authorization'] = `Bearer ${state.validToken}`;
});
async function main() {
const response = await instance.get('/protected');
assertEqual(response.status, 200, 'Response status should be 200');
assertEqual(state.refreshCount, 1, 'Refresh should have been called exactly once');
console.log(' PASS basic-refresh');
}
main();