[User] Wenn curerntSession gelöscht wird, wird man automatisch ausgelogt

Wenn beim wechseln der Seite kein Token gefunden wird, wird ebenfalls automatisch ausgeloggt.
This commit is contained in:
Tim Gröger 2020-10-30 13:27:33 +01:00
parent cc27307835
commit c9a5b6d165
3 changed files with 42 additions and 23 deletions

View File

@ -11,22 +11,25 @@ export default boot<Store<StateInterface>>(({ 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();

View File

@ -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<SessionInterface> = {
};
const actions: ActionTree<SessionInterface, StateInterface> = {
getSessions({ commit, rootGetters }) {
console.log(rootGetters);
getSessions({ commit, rootState, dispatch }) {
commit('setLoading', true);
axios
.get('/auth')
.then(response => {
.then((response: AxiosResponse<FG.Session[]>) => {
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<SessionInterface, StateInterface> = {
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(() => {
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);

View File

@ -44,13 +44,6 @@ const mutations: MutationTree<UserStateInterface> = {
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<UserStateInterface, StateInterface> = {
},
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');
}
}
};