From 7e53eea7f2a41a3766c72001c532b23d826ae655 Mon Sep 17 00:00:00 2001 From: Bubka <858858+Bubka@users.noreply.github.com> Date: Thu, 16 Jan 2020 17:12:25 +0100 Subject: [PATCH] Use async/await --- resources/js/views/Accounts.vue | 43 ++++++++++++---------------- resources/js/views/auth/Login.vue | 29 ++++++++++++------- resources/js/views/auth/Register.vue | 33 ++++++++++++--------- 3 files changed, 57 insertions(+), 48 deletions(-) diff --git a/resources/js/views/Accounts.vue b/resources/js/views/Accounts.vue index 5d55337c..a657ecd1 100644 --- a/resources/js/views/Accounts.vue +++ b/resources/js/views/Accounts.vue @@ -151,29 +151,22 @@ }, methods: { - getAccount: function (id) { - let accountId = id + async getAccount(id) { + try { + const { data } = await axios.get('api/twofaccounts/' + id) - axios.get('api/twofaccounts/' + id).then(response => { + this.twofaccount.id = data.id + this.twofaccount.service = data.service + this.twofaccount.account = data.account + this.twofaccount.icon = data.icon - this.twofaccount.id = response.data.id - this.twofaccount.service = response.data.service - this.twofaccount.account = response.data.account - this.twofaccount.icon = response.data.icon - - this.$refs.OneTimePassword.AccountId = response.data.id + this.$refs.OneTimePassword.AccountId = data.id this.$refs.OneTimePassword.getOTP() this.ShowTwofaccountInModal = true; - - }) - .catch(error => { - if (error.response.status === 404) { - this.$router.push({name: '404', params: { err : error.response.data.error }}); - } - else { - this.$router.push({ name: 'genericError', params: { err: error.response.data.message } }); - } - }); + } + catch (error) { + this.$router.push({ name: 'genericError', params: { err: error.response.data.message } }); + } }, deleteAccount: function (id) { @@ -188,19 +181,21 @@ } }, - logout(evt) { + async logout(evt) { if(confirm(this.$t('auth.confirm.logout'))) { - axios.post('api/logout').then(response => { + try { + await axios.get('api/logout') localStorage.removeItem('jwt'); localStorage.removeItem('user'); + delete axios.defaults.headers.common['Authorization']; this.$router.go('/login'); - }) - .catch(error => { + } + catch (error) { this.$router.push({ name: 'genericError', params: { err: error.response.data.message } }); - }); + } } } diff --git a/resources/js/views/auth/Login.vue b/resources/js/views/auth/Login.vue index 5c2343bb..c65673bd 100644 --- a/resources/js/views/auth/Login.vue +++ b/resources/js/views/auth/Login.vue @@ -52,23 +52,28 @@ validationErrors: {} } }, + methods : { - handleSubmit(e){ + async handleSubmit(e) { e.preventDefault() - axios.post('api/login', { - email: this.email, - password: this.password - }) - .then(response => { - localStorage.setItem('user',response.data.message.name) - localStorage.setItem('jwt',response.data.message.token) + try { + const { data } = await axios.post('api/login', { + email: this.email, + password: this.password + }) + + localStorage.setItem('user',data.message.name) + localStorage.setItem('jwt',data.message.token) if (localStorage.getItem('jwt') != null){ this.$router.go('/'); } - }) - .catch(error => { + } + catch (error) { + + this.validationErrors = {} + if( error.response.status === 401 ) { this.validationErrors['email'] = '' this.validationErrors['password'] = [ this.$t('auth.forms.password_do_not_match') ] @@ -79,9 +84,11 @@ else { this.$router.push({ name: 'genericError', params: { err: error.response.data.message } }); } - }); + } } + }, + beforeRouteEnter (to, from, next) { if (localStorage.getItem('jwt')) { return next('/'); diff --git a/resources/js/views/auth/Register.vue b/resources/js/views/auth/Register.vue index 9d67523d..75ed7914 100644 --- a/resources/js/views/auth/Register.vue +++ b/resources/js/views/auth/Register.vue @@ -73,7 +73,7 @@ .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' }); + this.$router.push({ name: 'flooded' }); } }) .catch(error => { @@ -82,28 +82,35 @@ }, methods : { - handleSubmit(e) { + async handleSubmit(e) { e.preventDefault() - axios.post('api/register', { - name: this.name, - email: this.email, - password: this.password, - password_confirmation : this.password_confirmation - }) - .then(response => { + try { + const { data } = await axios.post('api/register', { + name: this.name, + email: this.email, + password: this.password, + password_confirmation : this.password_confirmation + }) + + localStorage.setItem('user',data.message.name) + localStorage.setItem('jwt',data.message.token) + if (localStorage.getItem('jwt') != null){ - this.$router.go('/'); + this.$router.push({name: 'accounts'}) } - }) - .catch(error => { + } + catch (error) { + + this.validationErrors = {} + if( error.response.data.validation ) { this.validationErrors = error.response.data.validation } else { this.$router.push({ name: 'genericError', params: { err: error.response.data.message } }); } - }); + } } },