Guest User

Untitled

a guest
Jun 24th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. // todos-vue/src/backend/axios/index.js
  2.  
  3. // ...
  4. import { store } from './../../store'
  5. // ...
  6.  
  7. securedAxiosInstance.interceptors.request.use(config => {
  8. const method = config.method.toUpperCase()
  9. if (method !== 'OPTIONS' && method !== 'GET') {
  10. config.headers = {
  11. ...config.headers,
  12. 'X-CSRF-TOKEN': store.state.csrf
  13. }
  14. }
  15. return config
  16. })
  17.  
  18. securedAxiosInstance.interceptors.response.use(null, error => {
  19. if (error.response && error.response.config && error.response.status === 401) {
  20. // In case 401 is caused by expired access cookie - we'll do refresh request
  21. return plainAxiosInstance.post('/refresh', {}, { headers: { 'X-CSRF-TOKEN': store.state.csrf } })
  22. .then(response => {
  23. store.commit('refresh', response.data.csrf)
  24. // And after successful refresh - repeat the original request
  25. let retryConfig = error.response.config
  26. retryConfig.headers['X-CSRF-TOKEN'] = store.state.csrf
  27. return plainAxiosInstance.request(retryConfig)
  28. }).catch(error => {
  29. store.commit('unsetCurrentUser')
  30. // redirect to signin in case refresh request fails
  31. location.replace('/')
  32. return Promise.reject(error)
  33. })
  34. } else {
  35. return Promise.reject(error)
  36. }
  37. })
  38.  
  39. export { securedAxiosInstance, plainAxiosInstance }
Add Comment
Please, Sign In to add comment