51 lines
1.2 KiB
TypeScript
51 lines
1.2 KiB
TypeScript
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<any, any> = {
|
|
loginAction: async ({ commit }, payload: { username: string, password: string }): Promise<any> => {
|
|
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<any, any> = {
|
|
state: store,
|
|
actions,
|
|
mutations,
|
|
getters
|
|
};
|
|
|
|
export default authStore;
|