From 0749df73c8162ea87014fc43431e7873095dbe7a Mon Sep 17 00:00:00 2001 From: leandro <leandroalmada86@gmail.com> Date: Thu, 23 Sep 2021 11:30:49 +0200 Subject: [PATCH] add error in user store --- src/store/index.js | 16 ++++++++++++++-- src/views/registration/Login.vue | 14 ++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/store/index.js b/src/store/index.js index faebe1b6..ce5d58e6 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -30,6 +30,7 @@ export default new Vuex.Store({ map }, state: { + error: null, logged: false, user: false, configuration:null, @@ -43,6 +44,9 @@ export default new Vuex.Store({ }, mutations: { + error(state, data) { + return state.error = data + }, SET_PROJECTS(state, projects) { state.projects = projects; }, @@ -125,15 +129,23 @@ export default new Vuex.Store({ password: payload.password, }) .then((response) => { + commit('error', null) if (response && response.status === 200) { // * use stored previous route to go back after login if page not open on login at first - const routerHistory = router.options.routerHistory[0].name !== "login" ? router.options.routerHistory : "/" + let routerHistory = '' + if (router.options.routerHistory[0] != undefined){ + routerHistory = router.options.routerHistory[0].name !== "login" ? router.options.routerHistory : "/" + } else { + routerHistory = "/" + } commit("SET_USER", response.data.user); router.push(routerHistory[routerHistory.length - 1] || "/") dispatch("GET_USER_LEVEL_PROJECTS"); } }) - .catch(() => { + .catch((error) => { + if (error.response.status === 403) + commit('error', error.response.data.detail) commit("SET_USER", false); }); } diff --git a/src/views/registration/Login.vue b/src/views/registration/Login.vue index 05d38bd0..470d00f3 100644 --- a/src/views/registration/Login.vue +++ b/src/views/registration/Login.vue @@ -62,6 +62,7 @@ <script> +import { mapState } from "vuex"; export default { name: "Login", @@ -76,6 +77,7 @@ export default { }; }, computed: { + ...mapState(["error"]), LOGO_PATH:function () { return this.$store.state.configuration.VUE_APP_LOGO_PATH; }, @@ -87,11 +89,19 @@ export default { }, }, methods: { - login() { + async login() { this.$store.dispatch("LOGIN", { username: this.username_value, password: this.password_value, - }); + }) + .then(() => { + if (this.error != null){ + this.form.errors = "Les informations d'identification sont incorrectes."; + } + }) + .catch(() => { + this.form.errors = this.error + }); }, }, -- GitLab