Created
May 31, 2019 17:42
-
-
Save NiltonMorais/fa5a471fd954ed355e15f4d8fb6e1302 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 JwtToken from '../services/jwt-token' | |
| import store from '../store/store'; | |
| import config from '../config'; | |
| let logout = ()=>{ | |
| store.dispatch('clearAuth'); | |
| window.location.href = config.login_url; | |
| }; | |
| axios.interceptors.request.use( | |
| request => { | |
| request.headers.Authorization = JwtToken.getAuthorizationHeader(); | |
| return request; | |
| }, | |
| error => Promise.reject(error) | |
| ); | |
| axios.interceptors.response.use(response => { | |
| return response; | |
| }, error => { | |
| const originalRequest = error.config; | |
| const status = error.response ? error.response.status : 'No response'; | |
| if(status === 401 && !originalRequest._retry && (error.request.responseURL !== config.endpoint_login)) { | |
| originalRequest._retry = true; | |
| return JwtToken.refreshToken() | |
| .then(() => { | |
| return axios(originalRequest); | |
| }) | |
| .catch(() => { | |
| logout(); | |
| }); | |
| } | |
| return Promise.reject(error); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment