flaschengeist-frontend/src/plugins/user/store/session.ts

153 lines
4.1 KiB
TypeScript
Raw Normal View History

2020-10-16 06:45:40 +00:00
import { Module, MutationTree, ActionTree, GetterTree } from 'vuex';
import { LoginData, LoginResponse } from 'src/plugins/user/models';
2020-10-16 06:45:40 +00:00
import { StateInterface } from 'src/store';
import { axios } from 'src/boot/axios';
import { AxiosError, AxiosResponse } from 'axios';
import { Router } from 'src/router';
import { LocalStorage, Loading } from 'quasar';
2020-10-16 06:45:40 +00:00
export interface SessionInterface {
currentSession?: FG.Session;
sessions: FG.Session[];
2020-10-16 06:45:40 +00:00
loading: boolean;
}
function loadFromLocal() {
const session = LocalStorage.getItem<FG.Session>('currentSession');
if (session) session.expires = new Date(session.expires);
return session;
}
2020-10-16 06:45:40 +00:00
const state: SessionInterface = {
sessions: [],
currentSession: loadFromLocal() || undefined,
2020-10-16 06:45:40 +00:00
loading: false
};
const mutations: MutationTree<SessionInterface> = {
setCurrentSession(state, session: FG.Session) {
LocalStorage.set('currentSession', session);
state.currentSession = session;
},
clearCurrentSession(state) {
LocalStorage.remove('currentSession');
state.currentSession = undefined;
},
setSessions(state, sessions: FG.Session[]) {
2020-10-16 06:45:40 +00:00
state.sessions = sessions;
},
setLoading(state, value: boolean) {
2020-10-16 06:45:40 +00:00
state.loading = value;
}
};
const actions: ActionTree<SessionInterface, StateInterface> = {
login({ commit }, data: LoginData) {
Loading.show({
message: 'Du wirst angemeldet'
});
void axios
.post('/auth', data)
.then((response: AxiosResponse<LoginResponse>) => {
response.data.session.expires = new Date(response.data.session.expires);
commit('setCurrentSession', response.data.session);
commit('user/setCurrentUser', response.data.user, { root: true });
commit('user/setCurrentPermissions', response.data.permissions, {
root: true
});
2020-10-16 06:45:40 +00:00
})
.catch(error => console.warn(error))
2020-10-16 06:45:40 +00:00
.finally(() => {
void Router.push({ name: 'user-main' });
Loading.hide();
2020-10-16 06:45:40 +00:00
});
2020-10-16 11:07:31 +00:00
},
/**
* Logout from current session
*/
logout({ dispatch, rootState }) {
Loading.show({ message: 'Session wird abgemeldet' });
dispatch('deleteSession', rootState.session.currentSession?.token).finally(
() => {
Loading.hide();
}
);
},
/**
* Delete a given session
*/
deleteSession({ commit, dispatch, rootState }, token: string | null) {
if (token === null) return;
2020-10-16 11:07:31 +00:00
commit('setLoading', true);
axios
.delete(`/auth/${token}`)
2020-10-16 11:07:31 +00:00
.then(() => {
if (token === rootState.session.currentSession?.token) {
void dispatch('clearup');
} else {
commit('getSessions');
}
})
.catch((error: AxiosError) => {
if (!error.response || error.response.status != 401) throw error;
})
.finally(() => {
commit('setLoading', false);
});
},
clearup({ commit }) {
commit('clearCurrentSession');
commit('user/clearCurrentUser', null, { root: true });
void Router.push({ name: 'login' });
},
/**
* Get all sessions from current User
*/
getSessions({ commit, state, dispatch }) {
commit('setLoading', true);
axios
.get('/auth')
.then((response: AxiosResponse<FG.Session[]>) => {
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 === state.currentSession?.token;
});
if (currentSession) {
void dispatch('setCurrentSession', currentSession);
}
2020-10-16 11:07:31 +00:00
})
.catch(error => {
console.exception(error);
2020-10-16 11:07:31 +00:00
})
.finally(() => {
commit('setLoading', false);
});
2020-10-16 06:45:40 +00:00
}
};
const getters: GetterTree<SessionInterface, StateInterface> = {
currentSession(state) {
return state.currentSession;
},
sessions(state) {
2020-10-16 06:45:40 +00:00
return state.sessions;
},
loading(state) {
return state.loading;
2020-10-16 06:45:40 +00:00
}
};
const sessions: Module<SessionInterface, StateInterface> = {
namespaced: true,
state,
mutations,
actions,
getters
};
export default sessions;