apiss.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. export default function ({ $axios, redirect, app, store }, inject) {
  2. // Create a custom axios instance
  3. const apiss = $axios.create({
  4. // headers: {
  5. // common: {
  6. // Accept: 'text/plain, */*'
  7. // }
  8. // },
  9. baseURL: '/apiss',
  10. timeout: 10000,
  11. withCredentials: true
  12. })
  13. apiss.onRequest(config => {
  14. if (store.state.token) {
  15. config.headers.authorization = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJwLnNlZWNvZGVyLmNuIiwic3ViIjoiMTgwMDE1ODUyNjciLCJhdWQiOiJzZWVjLW1vb2MiLCJhdXRoX3RpbWUiOjE2MjI2OTAxMjUsImlhdCI6MTYyMjY5MDEyNSwiZXhwIjoxNjIyNzc2NTI1LCJ1c2VyX2luZm8iOnsicGhvbmUiOiIxODAwMTU4NTI2NyIsImVtYWlsIjoicWlubGl1QG5qdS5lZHUuY24iLCJpZCI6NDI5LCJuYW1lIjoia2Vubnk2N25qdSIsInJvbGUiOiJURUFDSEVSIn19.cl2Afo5332S5L2uFVULEzaiqvhLDpmBMFPaymDuDDfg"
  16. } else {
  17. delete config.headers.authorization
  18. }
  19. return config
  20. })
  21. apiss.setHeader('Content-Type', 'application/json')
  22. apiss.onResponse(response => {
  23. if (response.status === 401) {
  24. store.commit('saveToken', null)
  25. store.commit('saveUser', {})
  26. redirect('/unauthorized')
  27. }
  28. })
  29. apiss.onError(error => {
  30. if (error.response.status === 401) {
  31. store.commit('saveToken', null)
  32. store.commit('saveUser', {})
  33. redirect('/unauthorized')
  34. }
  35. })
  36. // Inject to context as $apiss
  37. inject('apiss', apiss)
  38. }