import { ActionTree, Module } from 'vuex'; import { RepositoryFactory } from '@/api/RepositoryFactory'; import AuthRepository from '@/api/AuthRepository'; const authRepository: AuthRepository = RepositoryFactory.get('auth'); const store = { token: undefined, isAdmin: false, lastLogin: undefined }; const actions: ActionTree = { loginAction: async ({ commit }, payload: { username: string, password: string }): Promise => { const response = await authRepository.login(payload); if (response && response.data.token) { commit('saveToken', response.data.token); } return response; }, }; const mutations = { saveToken: (state: any, token: any) => { state.token = token; state.lastLogin = new Date(); }, clearToken: (state: any) => { state.token = null; state.lastLogin = null; }, saveAdminPermission: (state: any, isAdmin: boolean) => state.isAdmin = isAdmin }; const getters = { token: (state: any) => state.token, lastLogin: (state: any) => state.lastLogin, isAdmin: (state: any) => state.isAdmin }; const authStore: Module = { state: store, actions, mutations, getters }; export default authStore;