From 90bf333934a1ddb207465ba89c00c82afe1d0a95 Mon Sep 17 00:00:00 2001 From: Bubka <858858+Bubka@users.noreply.github.com> Date: Sun, 26 Jan 2020 21:57:53 +0100 Subject: [PATCH] Remove error handler from direct axios calls, the interceptor will do the job --- resources/js/api.js | 15 +++++++++++---- resources/js/views/Accounts.vue | 18 +++++++----------- resources/js/views/auth/Register.vue | 19 ++++++++----------- resources/js/views/twofaccounts/Edit.vue | 20 ++++++++------------ 4 files changed, 34 insertions(+), 38 deletions(-) diff --git a/resources/js/api.js b/resources/js/api.js index 11517456..976fc4e8 100644 --- a/resources/js/api.js +++ b/resources/js/api.js @@ -32,14 +32,21 @@ Vue.axios.interceptors.request.use(function (request) { Vue.axios.interceptors.response.use(response => response, error => { - if ( error.response.status === 404 ) { + // Return the error when it has been asked + if( error.config.hasOwnProperty('returnError') && error.config.returnError === true ) { + return Promise.reject(error); + } + if( error.response.status === 422 ) { + return Promise.reject(error); + } + + // Otherwise we push to the error views + if ( error.response.status === 404 ) { router.push({name: '404', params: { err : error.response }}) } else { - - // router.push({ name: 'genericError', params: { err: error.response } }); - return Promise.reject(error) + router.push({ name: 'genericError', params: { err: error.response } }) } diff --git a/resources/js/views/Accounts.vue b/resources/js/views/Accounts.vue index 31e9d6b2..674a2074 100644 --- a/resources/js/views/Accounts.vue +++ b/resources/js/views/Accounts.vue @@ -159,21 +159,17 @@ } }, - logout(evt) { + async logout(evt) { if(confirm(this.$t('auth.confirm.logout'))) { - this.axios.get('api/logout') - .then(response => { - localStorage.removeItem('jwt'); - localStorage.removeItem('user'); + await this.axios.get('api/logout') - delete this.axios.defaults.headers.common['Authorization']; + localStorage.removeItem('jwt'); + localStorage.removeItem('user'); - this.$router.go('/login'); - }) - .catch(error => { - this.$router.push({ name: 'genericError', params: { err: error.response } }); - }); + delete this.axios.defaults.headers.common['Authorization']; + + this.$router.go('/login'); } } diff --git a/resources/js/views/auth/Register.vue b/resources/js/views/auth/Register.vue index 032bd0df..47999a43 100644 --- a/resources/js/views/auth/Register.vue +++ b/resources/js/views/auth/Register.vue @@ -71,18 +71,15 @@ } }, - created: function() { + async created() { // we check if a user account already exists - this.axios.post('api/checkuser') - .then(response => { - if( response.data.userCount > 0) { - this.errorMessage = this.$t('errors.already_one_user_registered') + ' ' + this.$t('errors.cannot_register_more_user') - this.$router.push({ name: 'flooded' }); - } - }) - .catch(error => { - this.$router.push({ name: 'genericError', params: { err: error.response } }); - }); + const { data } = await this.axios.post('api/checkuser') + + if( data.userCount > 0) { + this.errorMessage = this.$t('errors.already_one_user_registered') + ' ' + this.$t('errors.cannot_register_more_user') + this.$router.push({ name: 'flooded' }); + } + }, methods : { diff --git a/resources/js/views/twofaccounts/Edit.vue b/resources/js/views/twofaccounts/Edit.vue index 51e4192a..ba4eabfb 100644 --- a/resources/js/views/twofaccounts/Edit.vue +++ b/resources/js/views/twofaccounts/Edit.vue @@ -103,23 +103,19 @@ }, methods: { - getAccount () { + async getAccount () { - this.axios.get('/api/twofaccounts/' + this.$route.params.twofaccountId) - .then(response => { - this.form.fill(response.data) - this.twofaccountExists = true + const { data } = await this.axios.get('/api/twofaccounts/' + this.$route.params.twofaccountId) - // set account icon as temp icon - this.tempIcon = this.form.icon - }) - .catch(error => { - this.$router.push({ name: 'genericError', params: { err: error.response } }); - }); + this.form.fill(data) + this.twofaccountExists = true + // set account icon as temp icon + this.tempIcon = this.form.icon + }, - updateAccount() { + async updateAccount() { // Set new icon and delete old one if( this.tempIcon !== this.form.icon ) {