From ea3e503d13b7be477c064b02b9b2e5a9ef06c9a9 Mon Sep 17 00:00:00 2001
From: Pitu <heyitspitu@gmail.com>
Date: Fri, 29 Mar 2019 00:35:58 +0900
Subject: Added middleware for pages and switched to $axios

---
 src/site/pages/login.vue | 31 +++++++++++++------------------
 1 file changed, 13 insertions(+), 18 deletions(-)

(limited to 'src/site/pages/login.vue')

diff --git a/src/site/pages/login.vue b/src/site/pages/login.vue
index fe7d64a..c713674 100644
--- a/src/site/pages/login.vue
+++ b/src/site/pages/login.vue
@@ -95,32 +95,27 @@ export default {
 		return { title: 'Login' };
 	},
 	methods: {
-		login() {
+		async login() {
 			if (this.isLoading) return;
 			if (!this.username || !this.password) {
 				this.$showToast('Please fill both fields before attempting to log in.', true);
 				return;
 			}
 			this.isLoading = true;
-			this.axios.post(`${this.config.baseURL}/auth/login`, {
-				username: this.username,
-				password: this.password
-			}).then(res => {
-				this.$store.commit('token', res.data.token);
-				this.$store.commit('user', res.data.user);
-				/*
-				if (res.data.mfa) {
-					this.isMfaModalActive = true;
-					this.isLoading = false;
-				} else {
-					this.getUserData();
-				}
-				*/
+
+			try {
+				const data = await this.$axios.$post(`auth/login`, {
+					username: this.username,
+					password: this.password
+				});
+				this.$axios.setToken(data.token, 'Bearer');
+				this.$store.dispatch('login', { token: data.token, user: data.user });
 				this.redirect();
-			}).catch(err => {
+			} catch (error) {
+				this.$onPromiseError(error);
+			} finally {
 				this.isLoading = false;
-				this.$onPromiseError(err);
-			});
+			}
 		},
 		/*
 		mfa() {
-- 
cgit v1.2.3

