From e347129ba9501036b67968fb37b880a8c75248bf Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Fri, 5 Feb 2021 00:22:11 +0100 Subject: [PATCH] [pinia] Fixed handling of multiple calls to logout * Better handling if we are logged out --- src/boot/axios.ts | 17 +++++++++-------- src/plugins/balance/pages/Overview.vue | 3 +-- src/store/index.ts | 15 ++++++++++----- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/boot/axios.ts b/src/boot/axios.ts index 6553f09..75ab0b7 100644 --- a/src/boot/axios.ts +++ b/src/boot/axios.ts @@ -27,34 +27,35 @@ export default boot(({ router }) => { */ api.interceptors.response.use( (response) => response, - (error) => { + async (error) => { const store = useMainStore(); if (error) { const e = error; + const current = router.currentRoute.value; if ( e.code === 'ECONNABORTED' || (e.response && e.response.status >= 502 && e.response.status <= 504) ) { - const current = router.currentRoute.value; let next = current.path; if ((current.name == 'login' || current.name == 'offline') && current.query.redirect) next = current.query.redirect; - return router.push({ + await router.push({ name: 'offline', query: { redirect: next }, }); } else if (e.response && e.response.status == 401) { - if (router.currentRoute.value.name !== 'login') { - store.logout(); - return router.push({ + void store.logout(); + if (current.name !== 'login') { + await router.push({ name: 'login', - query: { logout: '.', redirect: router.currentRoute.value.fullPath }, + params: { logout: 'logout' }, + query: { redirect: current.path }, }); } } } - return Promise.reject(error); + throw error; } ); }); diff --git a/src/plugins/balance/pages/Overview.vue b/src/plugins/balance/pages/Overview.vue index e694361..acb9715 100644 --- a/src/plugins/balance/pages/Overview.vue +++ b/src/plugins/balance/pages/Overview.vue @@ -122,8 +122,7 @@ export default defineComponent({ else { if (row.receiver_id == null) return 'Angeschrieben'; else { - if (row.receiver_id === (userGetters.currentUser())?.userid) - return 'Bekommen von X'; + if (row.receiver_id === mainStore.currentUser.userid) return 'Bekommen von X'; else return 'Gesendet an X'; } } diff --git a/src/store/index.ts b/src/store/index.ts index 2d95bf5..09eb430 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -88,17 +88,22 @@ export const useMainStore = defineStore({ } }, - logout() { - if (!this.session) return false; + async logout() { + if (!this.session || !this.session.token) return false; - void api.delete(`/auth/${this.session.token}`).finally(() => { + LocalStorage.clear(); + + try { + await api.delete(`/auth/${this.session.token}`); + } catch (error) { + return false; + } finally { this.$patch({ session: undefined, user: undefined, }); SessionStorage.clear(); - }); - LocalStorage.remove('session'); + } return true; },