diff --git a/src/main.js b/src/main.js index ff536131353b225b2e3aab5ec32d148f4228ad4d..bbc1cc8c37851dbc2b8c1a1dcdf6d484c6cd57d3 100644 --- a/src/main.js +++ b/src/main.js @@ -104,15 +104,33 @@ const updateOnlineStatus = () => { /** * Regularly updates the user status if using external auth to keep the frontend updated with backend. */ +function handleLogout() { + if (store.state.user) { + store.dispatch('LOGOUT'); + store.commit('DISPLAY_MESSAGE', { + level: 'negative', + comment: `Vous avez été déconnecté du service d'authentification. + Reconnectez-vous ou continuez en mode anonyme.` + }); + store.dispatch('GET_USER_LEVEL_PROJECTS'); + } +} + const updateUserStatus = () => { setInterval(() => { if (navigator.onLine) { axios .get(`${store.state.configuration.VUE_APP_DJANGO_API_BASE}user_info/`) .then((response) => { - if (response && response.status === 200) { - const user = response.data.user; - if (!store.state.user && user) { + const user = response.data?.user || null; + + if (response.status === 302) { + console.warn("Redirection détectée vers :", response.headers.location || response.request.responseURL); + } + + if (response.status === 200 && user) { + // Cas où l'utilisateur est bien authentifié + if (!store.state.user) { store.commit('SET_USER', user); store.commit('DISPLAY_MESSAGE', { level: 'positive', @@ -121,28 +139,15 @@ const updateUserStatus = () => { store.dispatch('GET_USER_LEVEL_PERMISSIONS'); store.dispatch('GET_USER_LEVEL_PROJECTS'); } - } else if (store.state.user) { + } else if (!user) { // Lorsqu'un utilisateur est déconnecté du SSO, la requête de vérification de session envoyée à l'API de l'application échoue avec une erreur 400. // On force la déconnexion de l'utilisateur dans ce cas. - store.dispatch('LOGOUT') - store.commit('DISPLAY_MESSAGE', { - level: 'negative', - comment: `Vous avez été déconnecté du service d'authentification. - Reconnectez-vous ou continuez en mode anonyme.` - }); + handleLogout(); } }) - .catch(() => { - if (store.state.user) { - store.commit('SET_USER', false); - store.commit('DISPLAY_MESSAGE', { - level: 'negative', - comment: `Vous avez été déconnecté du service d'authentification. - Reconnectez-vous ou continuez en mode anonyme.` - }); - store.dispatch('GET_USER_LEVEL_PERMISSIONS'); - store.dispatch('GET_USER_LEVEL_PROJECTS'); - } + .catch(error => { + console.error("Erreur API :", error); + handleLogout(); }); } }, 10000);