2020-10-16 06:45:40 +00:00
|
|
|
import { Module, MutationTree, ActionTree, GetterTree } from 'vuex';
|
|
|
|
import { StateInterface } from 'src/store';
|
2020-10-18 23:45:06 +00:00
|
|
|
import { axios } from 'src/boot/axios';
|
2020-10-30 12:27:33 +00:00
|
|
|
import { AxiosResponse } from 'axios';
|
|
|
|
import { Router } from 'src/router';
|
2020-10-18 23:45:06 +00:00
|
|
|
|
2020-10-16 06:45:40 +00:00
|
|
|
export interface SessionInterface {
|
2020-10-27 10:51:53 +00:00
|
|
|
sessions: FG.Session[];
|
2020-10-16 06:45:40 +00:00
|
|
|
loading: boolean;
|
|
|
|
}
|
2020-10-18 23:45:06 +00:00
|
|
|
|
2020-10-16 06:45:40 +00:00
|
|
|
const state: SessionInterface = {
|
|
|
|
sessions: [],
|
|
|
|
loading: false
|
|
|
|
};
|
|
|
|
|
|
|
|
const mutations: MutationTree<SessionInterface> = {
|
2020-10-27 10:51:53 +00:00
|
|
|
setSessions(state, sessions: FG.Session[]) {
|
2020-10-16 06:45:40 +00:00
|
|
|
state.sessions = sessions;
|
|
|
|
},
|
2020-10-27 10:51:53 +00:00
|
|
|
setLoading(state, value: boolean) {
|
2020-10-16 06:45:40 +00:00
|
|
|
state.loading = value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const actions: ActionTree<SessionInterface, StateInterface> = {
|
2020-10-30 12:27:33 +00:00
|
|
|
getSessions({ commit, rootState, dispatch }) {
|
2020-10-16 06:45:40 +00:00
|
|
|
commit('setLoading', true);
|
|
|
|
axios
|
2020-10-16 20:37:37 +00:00
|
|
|
.get('/auth')
|
2020-10-30 12:27:33 +00:00
|
|
|
.then((response: AxiosResponse<FG.Session[]>) => {
|
2020-10-16 11:07:31 +00:00
|
|
|
console.log(response.data);
|
2020-10-30 12:27:33 +00:00
|
|
|
response.data.forEach(session => {
|
|
|
|
session.expires = new Date(session.expires);
|
|
|
|
});
|
2020-10-16 06:45:40 +00:00
|
|
|
commit('setSessions', response.data);
|
2020-10-30 12:27:33 +00:00
|
|
|
const currentSession = response.data.find((session: FG.Session) => {
|
|
|
|
return session.token === rootState.user.session.token;
|
|
|
|
});
|
|
|
|
if (currentSession) {
|
|
|
|
void dispatch('user/setSession', currentSession, { root: true });
|
|
|
|
}
|
2020-10-16 06:45:40 +00:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.exception(error);
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
commit('setLoading', false);
|
|
|
|
});
|
2020-10-16 11:07:31 +00:00
|
|
|
},
|
2020-10-30 12:27:33 +00:00
|
|
|
deleteSession({ commit, dispatch, rootState }, token: string) {
|
2020-10-16 11:07:31 +00:00
|
|
|
commit('setLoading', true);
|
|
|
|
axios
|
2020-10-16 20:37:37 +00:00
|
|
|
.delete(`/auth/${token}`)
|
2020-10-16 11:07:31 +00:00
|
|
|
.then(() => {
|
2020-10-30 12:27:33 +00:00
|
|
|
if (token === rootState.user.session.token) {
|
|
|
|
void dispatch('user/setSession', null, { root: true });
|
|
|
|
Router.go(0);
|
|
|
|
} else {
|
|
|
|
void dispatch('getSessions');
|
|
|
|
}
|
2020-10-16 11:07:31 +00:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2020-10-18 23:45:06 +00:00
|
|
|
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> = {
|
2020-10-27 10:51:53 +00:00
|
|
|
sessions(state) {
|
2020-10-16 06:45:40 +00:00
|
|
|
return state.sessions;
|
2020-10-16 11:54:01 +00:00
|
|
|
},
|
2020-10-27 10:51:53 +00:00
|
|
|
loading(state) {
|
2020-10-16 11:54:01 +00:00
|
|
|
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;
|