axios.js 1.2 KB

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