diff --git a/.env.development b/.env.development index 1a75d96..886cdb9 100644 --- a/.env.development +++ b/.env.development @@ -1 +1 @@ -VUE_APP_API_LOCATION=http://localhost:5000/ \ No newline at end of file +VUE_APP_API_LOCATION=http://localhost:5000/api/ \ No newline at end of file diff --git a/src/App.vue b/src/App.vue index db985a1..f489655 100644 --- a/src/App.vue +++ b/src/App.vue @@ -31,9 +31,34 @@ export default { }, created() { // The basic app is created... Currently showing a loading screen (as soon as mounted) - this.$store.dispatch('storeUserData','testuser').then(() => { + + if (this.$api.haveToken) { + + this.$api.getMyInfo().then(({name}) => { + + this.$store.dispatch('storeUserData', name).then(() => { + this.$router.push('/').catch(() => {}); + this.$store.dispatch('setAppReady'); + + }); + + }).catch(({status, text}) => { + if (status === 401) { + this.$api.clearTokenFromLocalStorage(); + this.$router.push('/login').catch(() => {}); + this.$store.dispatch('setAppReady'); + + } else { + this.$showToast(text); + } + }); + + } else { + this.$router.push('/login').catch(() => {}); this.$store.dispatch('setAppReady'); - }); + + } + } } diff --git a/src/api/index.js b/src/api/index.js index 5372d34..488a066 100644 --- a/src/api/index.js +++ b/src/api/index.js @@ -35,6 +35,10 @@ export default new class { return !!localStorage.getItem(LOCAL_STORAGE_KEY) } + clearTokenFromLocalStorage() { + localStorage.removeItem(LOCAL_STORAGE_KEY); + } + _performApiCall(method, url, data, precheckToken, expectedStatus, errorTexts = COMMON_ERROR_CODES) { return new Promise((resolve, reject) => { @@ -86,7 +90,7 @@ export default new class { return new Promise((resolve, reject) => { - this._performApiCall('post', '/auth/login', {name, password}, false, 201, { + this._performApiCall('post', '/auth/login', {name, password}, false, 200, { 401: "Invalid credentials", ...COMMON_ERROR_CODES }).then((data) => { diff --git a/src/components/Navbar.vue b/src/components/Navbar.vue index 00d8b8e..d46ee0b 100644 --- a/src/components/Navbar.vue +++ b/src/components/Navbar.vue @@ -16,20 +16,55 @@ About + - - Lorem Ipsum dolor sit amaet - Logout + + + + + Logged in to... + + + + + Logout + + + diff --git a/src/main.js b/src/main.js index 6af8dfc..c83bd39 100644 --- a/src/main.js +++ b/src/main.js @@ -15,6 +15,41 @@ Vue.prototype.$api = api Vue.use(BootstrapVue) Vue.use(IconsPlugin) + +Vue.prototype.$showToast = function (text, type = 'error', local=true) { + + let options = {} + switch (type) { + case "error": + options = { + title: "Error!", + variant: "danger" + } + break; + case "user_error": + options = { + title: "Warning!", + variant: "warning" + } + break; + case "success": + options = { + title: "Success!", + variant: "success" + } + break; + } + + const bvToast = local ? this.$bvToast : this.$root.$bvToast + + bvToast.toast(text, { + ...options, + toaster: 'b-toaster-top-center', + solid: true, + appendToast: false + }) +} + new Vue({ router, store, diff --git a/src/views/Login.vue b/src/views/Login.vue index 814034c..974373f 100644 --- a/src/views/Login.vue +++ b/src/views/Login.vue @@ -2,48 +2,55 @@ - -
- -
-
+ + +
+ +
- - - - +
+ Username or password invalid! +
+
- - - + + + + -
- Login -
-
+ + + +
+ Login +
+ -
-
+
+
+
@@ -57,12 +64,43 @@ export default { form: { username: "", password: "" - } + }, + processing: false, + authFailed: false } }, methods: { performLogin() { + this.processing = true; + this.$api.performLogin(this.form.username, this.form.password).then(({name}) => { + this.$store.dispatch('storeUserData', name).then(() => { + + this.$router.push('/').catch(() => { + }); + this.processing = false; + + }); + + + }).catch(({status, text}) => { + + if (status === 401) { + this.authFailed = true; + } else { + this.$showToast(text); + } + + this.processing = false; + }); + }, + formChanged() { + this.authFailed = false; + } + }, + computed: { + formState() { + return this.authFailed ? false : null; } } }