File tree Expand file tree Collapse file tree 1 file changed +22
-11
lines changed
Expand file tree Collapse file tree 1 file changed +22
-11
lines changed Original file line number Diff line number Diff line change 1+ /* eslint-disable prefer-arrow-callback */
2+ /* eslint-disable func-names */
3+ /* eslint-disable no-param-reassign */
4+
15import Axios from 'axios' ;
26import 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
1627export default {
1728 install ( ) {
18- Vue . prototype . $http = axiosInstance ;
29+ Vue . prototype . $http = Axios ;
1930 } ,
2031} ;
You can’t perform that action at this time.
0 commit comments