-
-
Notifications
You must be signed in to change notification settings - Fork 810
Expand file tree
/
Copy pathapiAuth.ts
More file actions
31 lines (25 loc) · 1.01 KB
/
apiAuth.ts
File metadata and controls
31 lines (25 loc) · 1.01 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
import axios from 'axios';
import {CurrentUser} from './CurrentUser';
import {SnackReporter} from './snack/SnackManager';
export const initAxios = (currentUser: CurrentUser, snack: SnackReporter) => {
axios.interceptors.request.use((config) => {
if (!config.headers.has('x-gotify-key')) {
config.headers['x-gotify-key'] = currentUser.token();
}
return config;
});
axios.interceptors.response.use(undefined, (error) => {
if (!error.response) {
snack('Gotify server is not reachable, try refreshing the page.');
return Promise.reject(error);
}
const status = error.response.status;
if (status === 401) {
currentUser.tryAuthenticate().then(() => snack('Could not complete request.'));
}
if (status === 400 || status === 403 || status === 500) {
snack(error.response.data.error + ': ' + error.response.data.errorDescription);
}
return Promise.reject(error);
});
};