import { Module, MutationTree, ActionTree, GetterTree } from 'vuex'; import { LoginData, LoginResponse } from 'src/plugins/user/models'; 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'; export interface SessionInterface { currentSession?: FG.Session; sessions: FG.Session[]; loading: boolean; backendOffline: boolean; } function loadFromLocal() { const session = LocalStorage.getItem('currentSession'); if (session) session.expires = new Date(session.expires); return session; } const state: SessionInterface = { sessions: [], currentSession: loadFromLocal() || undefined, loading: false, backendOffline: false }; const mutations: MutationTree = { 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[]) { state.sessions = sessions; }, setLoading(state, value: boolean) { state.loading = value; }, setOffline(state, value: boolean) { state.backendOffline = value; } }; const actions: ActionTree = { login({ commit }, data: LoginData) { Loading.show({ message: 'Du wirst angemeldet' }); void axios .post('/auth', data) .then((response: AxiosResponse) => { 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 }); }) .catch(error => console.warn(error)) .finally(() => { void Router.push({ name: 'user-main' }); Loading.hide(); }); }, /** * 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; commit('setLoading', true); axios .delete(`/auth/${token}`) .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) => { 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); } }) .catch(error => { console.exception(error); }) .finally(() => { commit('setLoading', false); }); } }; const getters: GetterTree = { currentSession(state) { return state.currentSession; }, sessions(state) { return state.sessions; }, loading(state) { return state.loading; } }; const sessions: Module = { namespaced: true, state, mutations, actions, getters }; export default sessions;