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

86 lines
1.9 KiB
TypeScript
Raw Normal View History

2020-10-16 06:45:40 +00:00
import { Module, MutationTree, ActionTree, GetterTree } from 'vuex';
import { StateInterface } from 'src/store';
import { axios } from 'boot/axios';
import { LoginData } from 'src/plugins/user/models';
import { AxiosResponse } from 'axios';
import { LocalStorage } from 'quasar';
import { Router } from 'src/router';
export interface SessionInterface {
sessions: Session[];
loading: boolean;
}
export interface Session {
browser: string;
expires: string;
lifetime: number;
platform: string;
token: string;
}
const state: SessionInterface = {
sessions: [],
loading: false
};
const mutations: MutationTree<SessionInterface> = {
setSessions(state, sessions: Session[]) {
state.sessions = sessions;
},
setLoading(state, value: boolean) {
2020-10-16 06:45:40 +00:00
state.loading = value;
}
};
const actions: ActionTree<SessionInterface, StateInterface> = {
getSessions({ commit, rootGetters }) {
console.log(rootGetters);
commit('setLoading', true);
axios
.get('/auth')
2020-10-16 06:45:40 +00:00
.then(response => {
2020-10-16 11:07:31 +00:00
console.log(response.data);
2020-10-16 06:45:40 +00:00
commit('setSessions', response.data);
})
.catch(error => {
console.exception(error);
})
.finally(() => {
commit('setLoading', false);
});
2020-10-16 11:07:31 +00:00
},
deleteSession({ commit, dispatch, rootGetters }, token) {
commit('setLoading', true);
console.log('axios', axios);
2020-10-16 11:07:31 +00:00
axios
.delete(`/auth/${token}`)
2020-10-16 11:07:31 +00:00
.then(() => {
void dispatch('getSessions');
})
.catch(error => {
console.exception();
})
.finally(() => {
commit('setLoading', false);
});
2020-10-16 06:45:40 +00:00
}
};
const getters: GetterTree<SessionInterface, StateInterface> = {
sessions(state) {
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;