[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 user = store.state.user.user;
const session = store.state.user.session; 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[] = []; let permissions: string[] = [];
user.roles.forEach(role => { user.roles.forEach(role => {
permissions = permissions.concat(role.permissions); permissions = permissions.concat(role.permissions);
}); });
if (to.name != 'login') { if (to.name != 'login') {
if(to.matched.every((record: RouteRecord) => { if (session.expires >= new Date() || session.token === '') {
if (!('meta' in record) || store.dispatch('user/doLogout').catch(error => {
!('permission' in record.meta)) 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 true;
return permissions.includes((<{permission: string}>record.meta).permission); return permissions.includes(
(<{ permission: string }>record.meta).permission
);
}) })
) { ) {
next(); next();

View File

@ -1,6 +1,8 @@
import { Module, MutationTree, ActionTree, GetterTree } from 'vuex'; import { Module, MutationTree, ActionTree, GetterTree } from 'vuex';
import { StateInterface } from 'src/store'; import { StateInterface } from 'src/store';
import { axios } from 'src/boot/axios'; import { axios } from 'src/boot/axios';
import { AxiosResponse } from 'axios';
import { Router } from 'src/router';
export interface SessionInterface { export interface SessionInterface {
sessions: FG.Session[]; sessions: FG.Session[];
@ -22,14 +24,22 @@ const mutations: MutationTree<SessionInterface> = {
}; };
const actions: ActionTree<SessionInterface, StateInterface> = { const actions: ActionTree<SessionInterface, StateInterface> = {
getSessions({ commit, rootGetters }) { getSessions({ commit, rootState, dispatch }) {
console.log(rootGetters);
commit('setLoading', true); commit('setLoading', true);
axios axios
.get('/auth') .get('/auth')
.then(response => { .then((response: AxiosResponse<FG.Session[]>) => {
console.log(response.data); console.log(response.data);
response.data.forEach(session => {
session.expires = new Date(session.expires);
});
commit('setSessions', response.data); 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 => { .catch(error => {
console.exception(error); console.exception(error);
@ -38,13 +48,17 @@ const actions: ActionTree<SessionInterface, StateInterface> = {
commit('setLoading', false); commit('setLoading', false);
}); });
}, },
deleteSession({ commit, dispatch }, token: string) { deleteSession({ commit, dispatch, rootState }, token: string) {
commit('setLoading', true); commit('setLoading', true);
console.log('axios', axios);
axios axios
.delete(`/auth/${token}`) .delete(`/auth/${token}`)
.then(() => { .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 => { .catch(error => {
console.exception(error); console.exception(error);

View File

@ -44,13 +44,6 @@ const mutations: MutationTree<UserStateInterface> = {
setUser(state, data: FG.User) { setUser(state, data: FG.User) {
state.user = data; 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) { setSession(state, data: FG.Session) {
state.session = data; state.session = data;
}, },
@ -143,12 +136,21 @@ const actions: ActionTree<UserStateInterface, StateInterface> = {
}, },
loadFromLocalStorage({ commit }) { loadFromLocalStorage({ commit }) {
console.log('load from store');
let data = LocalStorage.getItem('user'); let data = LocalStorage.getItem('user');
commit('setUser', data ? data : empty_user); commit('setUser', data ? data : empty_user);
data = LocalStorage.getItem('session'); data = LocalStorage.getItem('session');
commit('setSession', data ? data : empty_session); commit('setSession', data ? data : empty_session);
commit('showState'); commit('showState');
},
setSession({ commit }, session: FG.Session) {
if (session) {
commit('setSession', session);
LocalStorage.set('session', session);
} else {
commit('setSession', empty_session);
LocalStorage.remove('session');
}
} }
}; };