axios.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import {TYPE_ALERT} from "~/types/enums";
  2. import Page from "~/services/store/page";
  3. export default function ({ $axios, redirect, store }) {
  4. $axios.onRequest(config => {
  5. if(!config.params.noXaccessId){
  6. $axios.setHeader('x-accessid', `${store.state.profile.access.id}`)
  7. if (store.state.profile.access.switchId) {
  8. $axios.setHeader('x-switch-user', `${store.state.profile.access.switchId}`)
  9. }
  10. $axios.setToken(`${store.state.profile.access.bearer}`, 'Bearer')
  11. }else{
  12. delete config.headers.common['Authorization']
  13. delete config.headers.common['x-accessid']
  14. $axios.setHeader('x-accessid', false)
  15. $axios.setToken(false)
  16. }
  17. })
  18. $axios.onResponse(response => {
  19. })
  20. $axios.onError((error) => {
  21. // In case of unauthorized, redirect to a specific page
  22. if (error.response.status === 401) {
  23. redirect('/login')
  24. }
  25. if (error.response.status === 403) {
  26. console.debug('forbidden')
  27. }
  28. if (error.response.status === 500) {
  29. new Page(store).addAlerts(TYPE_ALERT.ALERT, [error.response.data['hydra:description']])
  30. }
  31. })
  32. }