From d38fa377131a9989af872c26f4b4505bae8396dc Mon Sep 17 00:00:00 2001 From: Haris Razis Date: Thu, 31 Dec 2020 15:29:54 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=9A=20Check=20if=20user=20is=20already?= =?UTF-8?q?=20logged=20in?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/src/main.ts | 3 ++- web/src/store/index.ts | 10 ++++------ web/src/views/Home.vue | 11 +++++++++++ web/src/views/Login.vue | 3 ++- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/web/src/main.ts b/web/src/main.ts index 28688bc..88e3751 100644 --- a/web/src/main.ts +++ b/web/src/main.ts @@ -8,5 +8,6 @@ import 'bulma/css/bulma.css'; import '@fortawesome/fontawesome-free/css/all.css'; import '@fortawesome/fontawesome-free/js/all.js'; -axios.defaults.withCredentials = true; +axios.defaults.baseURL = 'http://localhost:8000'; + createApp(App).use(store).use(router).mount('#app') diff --git a/web/src/store/index.ts b/web/src/store/index.ts index 41f41f8..a3e5290 100644 --- a/web/src/store/index.ts +++ b/web/src/store/index.ts @@ -26,7 +26,7 @@ export default createStore({ return new Promise((resolve, reject) => { axios({ method: 'post', - url: 'http://localhost:8000/auth/login', + url: '/auth/login', data: qs.stringify({...user}) }) .then((resp: AxiosResponse) => { @@ -43,7 +43,7 @@ export default createStore({ return new Promise((resolve, reject) => { axios({ method: 'post', - url: 'http://localhost:8000/auth/logout' + url: '/auth/logout' }) .then((resp: AxiosResponse) => { commit("logout"); @@ -56,19 +56,17 @@ export default createStore({ }) }) }, - getUser({commit}) { + getCurrentUser({commit}) { return new Promise((resolve, reject) => { axios({ method: 'get', - url: 'http://localhost:8000/auth/current_user' + url: '/auth/current_user' }) .then((resp: AxiosResponse) => { - console.log(resp) commit("auth_success", resp.data); resolve(resp) }) .catch((err: Error) => { - console.log(err) commit('auth_error') reject(err) }) diff --git a/web/src/views/Home.vue b/web/src/views/Home.vue index 22740f3..662fce9 100644 --- a/web/src/views/Home.vue +++ b/web/src/views/Home.vue @@ -48,5 +48,16 @@ import ShapeDividerTop from '@/components/ShapeDividerTop.vue'; }) export default class Home extends Vue { + mounted() { + this.getCurrentUser(); + } + + private getCurrentUser() { + this.$store.dispatch('getCurrentUser') + .then(() => this.$router.push('/dashboard')) + .catch((err: any) => { + this.$router.push('/') + }) + } } diff --git a/web/src/views/Login.vue b/web/src/views/Login.vue index 8725c99..2e23659 100644 --- a/web/src/views/Login.vue +++ b/web/src/views/Login.vue @@ -61,7 +61,8 @@ export default class Login extends Vue { this.$store.dispatch('login', user) .then(() => this.$router.push('/dashboard')) .catch((err: any) => { - this.msg = err.response.data.errors.message || err.message + console.log(err) + this.msg = err.response.data.errors.message || err.message || 'Something went wrong!' this.$router.push('/login') }) }