Last active
May 16, 2019 11:56
-
-
Save wilcorrea/2ab664a17d7f9afe74b8d5e0735333a4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios from 'axios' | |
// window.axios = require('axios') | |
import config from '@/config' | |
import JwtToken from '@/services/jwt-token' | |
import $store from '@/store' | |
import config from '@/config' | |
const http = axios.create({ | |
baseURL: config.apiUrl, | |
// timeout: 1000, | |
headers: {'X-Custom-With': 'XMLHttpRequest'} | |
}); | |
let logout = () => { | |
$store.dispatch('clearAuth') | |
// window.location.href = config.login_url; | |
} | |
http.interceptors.request.use( | |
request => { | |
request.headers.Authorization = JwtToken.getAuthorizationHeader() | |
return request | |
}, | |
error => Promise.reject(error) | |
) | |
http.interceptors.response.use(function (response) { | |
return response.data | |
}, function (error) { | |
const originalRequest = error.config | |
if (error.response.status === 401 && !originalRequest._retry && (error.request.responseURL !== config.endpoint_login) && (error.response.data.error !== 'disabled.device')) { | |
originalRequest._retry = true | |
return JwtToken.refreshToken() | |
.then(() => { | |
return this.$http(originalRequest) | |
}) | |
.catch(() => { | |
logout() | |
}) | |
} | |
return Promise.reject(error) | |
}) | |
export default http; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment