Use async/await

This commit is contained in:
Bubka 2020-01-16 17:12:25 +01:00
parent 2fc1ec4020
commit 7e53eea7f2
3 changed files with 57 additions and 48 deletions

View File

@ -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 } });
});
}
}
}

View File

@ -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('/');

View File

@ -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 } });
}
});
}
}
},