Skip to content

Commit 2921420

Browse files
committed
axios bearer header setup for access, refresh tokens
1 parent 637e656 commit 2921420

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

frontend/src/plugins/http.js

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,31 @@
1+
/* eslint-disable prefer-arrow-callback */
2+
/* eslint-disable func-names */
3+
/* eslint-disable no-param-reassign */
4+
15
import Axios from 'axios';
26
import Vue from 'vue';
7+
import { getAccessToken, getRefreshToken, handleAccessTokenExpiration } from '../auth/tokens';
38

4-
function createAxiosInstance(baseURL) {
5-
return Axios.create({
6-
baseURL,
7-
headers: {
8-
'Content-Type': 'application/json',
9-
Authorization: `Bearer ${localStorage.token}`,
10-
},
11-
});
12-
}
9+
Axios.defaults.baseURL = process.env.VUE_APP_BACKEND_BASE_URL;
1310

14-
const axiosInstance = createAxiosInstance(process.env.VUE_APP_BACKEND_BASE_URL);
11+
Axios.interceptors.request.use(async function (config) {
12+
if (config.url === '/auth/refresh') {
13+
config.headers.Authorization = `Bearer ${getRefreshToken()}`;
14+
} else if (getAccessToken()) {
15+
await handleAccessTokenExpiration();
16+
if (getAccessToken()) {
17+
config.headers.Authorization = `Bearer ${getAccessToken()}`;
18+
} else {
19+
return { headers: {}, method: config.method, url: '' };
20+
}
21+
}
22+
return config;
23+
}, function (error) {
24+
return Promise.reject(error);
25+
});
1526

1627
export default {
1728
install() {
18-
Vue.prototype.$http = axiosInstance;
29+
Vue.prototype.$http = Axios;
1930
},
2031
};

0 commit comments

Comments
 (0)