[pinia] Fixed handling of multiple calls to logout

* Better handling if we are logged out
This commit is contained in:
Ferdinand Thiessen 2021-02-05 00:22:11 +01:00
parent fd45a46c01
commit e347129ba9
3 changed files with 20 additions and 15 deletions

View File

@ -27,34 +27,35 @@ export default boot(({ router }) => {
*/
api.interceptors.response.use(
(response) => response,
(error) => {
async (error) => {
const store = useMainStore();
if (error) {
const e = <AxiosError>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 = <string>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;
}
);
});

View File

@ -122,8 +122,7 @@ export default defineComponent({
else {
if (row.receiver_id == null) return 'Angeschrieben';
else {
if (row.receiver_id === (<FG.User>userGetters.currentUser())?.userid)
return 'Bekommen von X';
if (row.receiver_id === mainStore.currentUser.userid) return 'Bekommen von X';
else return 'Gesendet an X';
}
}

View File

@ -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;
},