started the authentication

basic routing
This commit is contained in:
Torben Indorf
2020-02-09 09:48:43 +01:00
parent d07dc6e8a6
commit e2568a1bbe
19 changed files with 326 additions and 61 deletions
+50
View File
@@ -0,0 +1,50 @@
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;
-11
View File
@@ -1,11 +0,0 @@
import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
export default new Vuex.Store({
state: {},
mutations: {},
actions: {},
modules: {}
});
+30
View File
@@ -0,0 +1,30 @@
import Vue from 'vue';
import Vuex from 'vuex';
import VuexPersistence from 'vuex-persist';
import authStore from "@/store/auth.store";
Vue.use(Vuex);
// set up persistence storage plugin
const vuexPersistence = new VuexPersistence({
key: process.env.VUE_APP_TITLE,
storage: window.localStorage,
reducer: (state: any) => ({
auth: {
token: state.auth.token,
user: state.auth.user
}
}),
filter: (mutation) => ['auth/saveLogin'].includes(mutation.type)
});
export default new Vuex.Store({
state: {},
mutations: {},
actions: {},
modules: {
auth: {
namespaced: true,
...authStore
}
}
});