Move axios loader to bootstrap.js

This commit is contained in:
Bubka 2020-01-19 22:31:23 +01:00
parent 8e4f8c4f8a
commit 8940efc225
4 changed files with 32 additions and 58 deletions

1
resources/js/app.js vendored
View File

@ -1,7 +1,6 @@
import Vue from 'vue' import Vue from 'vue'
import router from './routes' import router from './routes'
import i18n from './langs/i18n' import i18n from './langs/i18n'
import axios from './packages/axios'
import FontAwesome from './packages/fontawesome' import FontAwesome from './packages/fontawesome'
import App from './components/App' import App from './components/App'

View File

@ -37,6 +37,36 @@ if (token) {
console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token'); console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token');
} }
window.axios.interceptors.request.use(function (request) {
const authToken = localStorage.getItem('jwt')
if(authToken) {
request.headers.common['Authorization'] = 'Bearer ' + authToken
}
request.headers.common['Content-Type'] = 'application/json'
return request
})
window.axios.interceptors.response.use(response => response, error => {
const { status } = error.response
if (status >= 500) {
router.push({name: 'genericError', params: { err : error.response }})
}
// if (status === 404) {
// router.push({name: '404'})
// }
return Promise.reject(error)
})
/** /**
* Echo exposes an expressive API for subscribing to channels and listening * Echo exposes an expressive API for subscribing to channels and listening
* for events that are broadcast by Laravel. Echo and event broadcasting * for events that are broadcast by Laravel. Echo and event broadcasting

View File

@ -1,56 +0,0 @@
import axios from 'axios'
import router from '../routes'
/**
* We'll load the axios HTTP library which allows us to easily issue requests
* to our Laravel back-end. This library automatically handles sending the
* CSRF token as a header based on the value of the "XSRF" token cookie.
*/
// window.axios = require('axios');
axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
/**
* Next we will register the CSRF Token as a common header with Axios so that
* all outgoing HTTP requests automatically have it attached. This is just
* a simple convenience so we don't have to attach every token manually.
*/
let csrfToken = document.head.querySelector('meta[name="csrf-token"]');
if (csrfToken) {
axios.defaults.headers.common['X-CSRF-TOKEN'] = csrfToken.content;
} else {
console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token');
}
axios.interceptors.request.use(request => {
const token = localStorage.getItem('jwt')
if(token) {
request.headers.common['Authorization'] = 'Bearer ' + token
}
request.headers.common['Content-Type'] = 'application/json'
return request
})
// Response interceptor
axios.interceptors.response.use(response => response, error => {
const { status } = error.response
if (status >= 500) {
router.push({name: 'genericError', params: { err : error.response }})
}
if (status === 404) {
router.push({name: '404'})
}
return Promise.reject(error)
})

View File

@ -81,7 +81,7 @@
<script> <script>
// import axios from '../packages/axios'
import Modal from '../components/Modal' import Modal from '../components/Modal'
import TwofaccountShow from '../components/TwofaccountShow' import TwofaccountShow from '../components/TwofaccountShow'
import OneTimePassword from '../components/OneTimePassword' import OneTimePassword from '../components/OneTimePassword'
@ -127,6 +127,7 @@
icon : data.icon icon : data.icon
}) })
}) })
this.showAccounts = this.accounts.length > 0 ? true : false this.showAccounts = this.accounts.length > 0 ? true : false
this.showNoAccount = !this.showAccounts this.showNoAccount = !this.showAccounts
}) })