From c9a5b6d165156fde4f10e9cfc0ec93115ebf5fbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Gr=C3=B6ger?= Date: Fri, 30 Oct 2020 13:27:33 +0100 Subject: [PATCH] =?UTF-8?q?[User]=20Wenn=20curerntSession=20gel=C3=B6scht?= =?UTF-8?q?=20wird,=20wird=20man=20automatisch=20ausgelogt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Wenn beim wechseln der Seite kein Token gefunden wird, wird ebenfalls automatisch ausgeloggt. --- src/boot/login.ts | 21 ++++++++++++--------- src/plugins/user/store/session.ts | 26 ++++++++++++++++++++------ src/plugins/user/store/user.ts | 18 ++++++++++-------- 3 files changed, 42 insertions(+), 23 deletions(-) diff --git a/src/boot/login.ts b/src/boot/login.ts index a6899c6..8c5003f 100644 --- a/src/boot/login.ts +++ b/src/boot/login.ts @@ -11,22 +11,25 @@ export default boot>(({ router, store }) => { const user = store.state.user.user; const session = store.state.user.session; - if (session.expires >= new Date()) { - store.dispatch('user/doLogout').catch(error => {console.warn(error)}); - return next({ name: 'login', query: { redirect: to.fullPath } }); - } - let permissions: string[] = []; user.roles.forEach(role => { permissions = permissions.concat(role.permissions); }); if (to.name != 'login') { - if(to.matched.every((record: RouteRecord) => { - if (!('meta' in record) || - !('permission' in record.meta)) + if (session.expires >= new Date() || session.token === '') { + store.dispatch('user/doLogout').catch(error => { + console.warn(error); + }); + return next({ name: 'login', query: { redirect: to.fullPath } }); + } + if ( + to.matched.every((record: RouteRecord) => { + if (!('meta' in record) || !('permission' in record.meta)) return true; - return permissions.includes((<{permission: string}>record.meta).permission); + return permissions.includes( + (<{ permission: string }>record.meta).permission + ); }) ) { next(); diff --git a/src/plugins/user/store/session.ts b/src/plugins/user/store/session.ts index 2333548..1727b3a 100644 --- a/src/plugins/user/store/session.ts +++ b/src/plugins/user/store/session.ts @@ -1,6 +1,8 @@ import { Module, MutationTree, ActionTree, GetterTree } from 'vuex'; import { StateInterface } from 'src/store'; import { axios } from 'src/boot/axios'; +import { AxiosResponse } from 'axios'; +import { Router } from 'src/router'; export interface SessionInterface { sessions: FG.Session[]; @@ -22,14 +24,22 @@ const mutations: MutationTree = { }; const actions: ActionTree = { - getSessions({ commit, rootGetters }) { - console.log(rootGetters); + getSessions({ commit, rootState, dispatch }) { commit('setLoading', true); axios .get('/auth') - .then(response => { + .then((response: AxiosResponse) => { console.log(response.data); + response.data.forEach(session => { + session.expires = new Date(session.expires); + }); commit('setSessions', response.data); + const currentSession = response.data.find((session: FG.Session) => { + return session.token === rootState.user.session.token; + }); + if (currentSession) { + void dispatch('user/setSession', currentSession, { root: true }); + } }) .catch(error => { console.exception(error); @@ -38,13 +48,17 @@ const actions: ActionTree = { commit('setLoading', false); }); }, - deleteSession({ commit, dispatch }, token: string) { + deleteSession({ commit, dispatch, rootState }, token: string) { commit('setLoading', true); - console.log('axios', axios); axios .delete(`/auth/${token}`) .then(() => { - void dispatch('getSessions'); + if (token === rootState.user.session.token) { + void dispatch('user/setSession', null, { root: true }); + Router.go(0); + } else { + void dispatch('getSessions'); + } }) .catch(error => { console.exception(error); diff --git a/src/plugins/user/store/user.ts b/src/plugins/user/store/user.ts index 322a23b..6f35d37 100644 --- a/src/plugins/user/store/user.ts +++ b/src/plugins/user/store/user.ts @@ -44,13 +44,6 @@ const mutations: MutationTree = { setUser(state, data: FG.User) { state.user = data; }, - updateUser(state, data: { [index: string]: string }) { - Object.keys(data).forEach(key => { - if ((<{ [index: string]: any }>state.user)[key] === data[key]) { - (<{ [index: string]: any }>state.user)[key] = data[key]; - } - }); - }, setSession(state, data: FG.Session) { state.session = data; }, @@ -143,12 +136,21 @@ const actions: ActionTree = { }, loadFromLocalStorage({ commit }) { - console.log('load from store'); let data = LocalStorage.getItem('user'); commit('setUser', data ? data : empty_user); data = LocalStorage.getItem('session'); commit('setSession', data ? data : empty_session); commit('showState'); + }, + + setSession({ commit }, session: FG.Session) { + if (session) { + commit('setSession', session); + LocalStorage.set('session', session); + } else { + commit('setSession', empty_session); + LocalStorage.remove('session'); + } } };