diff --git a/src/store/index.js b/src/store/index.js index 05b9065acb524d581a04dfbb4e4fea37a8e64fc4..232b6f380fccca09a092b8ab1666168d7c42c360 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -179,7 +179,12 @@ export default new Vuex.Store({ } }, - USER_INFO({ state, commit, dispatch }) { + async USER_INFO({ state, commit, dispatch }) { + const token = new URLSearchParams(window.location.search).get('token'); + // if user was previously connected through SSO, make sure he's logout before connecting through SSO, in case user changed + if (token && this.state.configuration.VUE_APP_LOGIN_URL) { + await dispatch('LOGOUT'); + } if (!state.user) { axios .get(`${this.state.configuration.VUE_APP_DJANGO_API_BASE}user_info/`) @@ -191,7 +196,6 @@ export default new Vuex.Store({ }) .catch(() => { //* if an url to redirect to an external authentification system is present, do not redirect to the login page - const token = new URLSearchParams(window.location.search).get('token'); if (!state.configuration.VUE_APP_LOGIN_URL) { const url = window.location.href; if (url.includes('projet-partage')) { @@ -211,9 +215,9 @@ export default new Vuex.Store({ } }, - async CONNECT_SSO_WITH_TOKEN({ commit, dispatch }, token) { + async CONNECT_SSO_WITH_TOKEN({ state, commit, dispatch }, token) { axios - .get(`${this.state.configuration.VUE_APP_DJANGO_API_BASE}login-token/?token=${token}`) + .get(`${state.configuration.VUE_APP_DJANGO_API_BASE}login-token/?token=${token}`) .then((response) => { if (response && (response.status === 200 || response.status === 201)) { const user = response.data; @@ -233,8 +237,8 @@ export default new Vuex.Store({ }); }, - async GET_USER_TOKEN({ commit }) { - const response = await axios.get(`${this.state.configuration.VUE_APP_DJANGO_API_BASE}get-token`); + async GET_USER_TOKEN({ state, commit }) { + const response = await axios.get(`${state.configuration.VUE_APP_DJANGO_API_BASE}get-token`); if ( response.status === 200 && response.data @@ -243,21 +247,24 @@ export default new Vuex.Store({ } }, - LOGOUT({ commit, dispatch }) { - axios - .get(`${this.state.configuration.VUE_APP_DJANGO_API_BASE}logout/`) + LOGOUT({ state, commit, dispatch }) { + return axios + .get(`${state.configuration.VUE_APP_DJANGO_API_BASE}logout/`) .then((response) => { if (response && response.status === 200) { commit('SET_USER', false); commit('SET_USER_LEVEL_PROJECTS', null); dispatch('GET_USER_LEVEL_PERMISSIONS'); - if (router.history.current.name !== 'index' && !window.location.pathname.includes('projet-partage')) { + if (router.history.current.name !== 'index' && + !window.location.pathname.includes('projet-partage') && + !state.configuration.VUE_APP_LOGIN_URL + ) { router.push('/'); } } }) .catch((error) => { - throw error; + console.error(error); }); },