Blame view

.history/prestige-vue-4.0.0/src/service/axios_20240409131927.js 1.35 KB
5b5ab7e5   함상기   20240409
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
39
40
  import axios from 'axios';
  import VueCookies from 'vue-cookies';
  import { refreshToken } from '../service/login'
  
  // axios.defaults.baseURL = 'http://localhost:8080';
  
  // Add a request interceptor
  axios.interceptors.request.use(async function (config) {
      // Do something before request is sent
      config.headers.Authorization = "Bearer " + VueCookies.get('token');
      config.headers.REFRESH_TOKEN = VueCookies.get('refresh_token');
    
      console.log(config);
      return config;
    }, function (error) {
      // Do something with request error
      return Promise.reject(error);
    });
  
  // Add a response interceptor
  axios.interceptors.response.use(function (response) {
      // Any status code that lie within the range of 2xx cause this function to trigger
      // Do something with response data
      return response;
    }, async function (error) {
      // Any status codes that falls outside the range of 2xx cause this function to trigger
      // Do something with response error
      console.log('에러일 경우', error.config);
      const errorAPI = error.config;
      if((error.code != null || error.response.data.status===401) && errorAPI.retry===undefined){
        errorAPI.retry = true;
        console.log('토큰이 이상한 오류일 경우');
        await refreshToken();
        return await axios(errorAPI);
      }
      return Promise.reject(error);
    });
  
  
  export default axios;