2020-10-16 06:45:40 +00:00
|
|
|
import { Module, MutationTree, ActionTree, GetterTree } from 'vuex';
|
2020-10-15 09:23:41 +00:00
|
|
|
import { StateInterface } from 'src/store';
|
|
|
|
import { axios } from 'boot/axios';
|
2020-10-16 06:45:40 +00:00
|
|
|
import { LoginData } from 'src/plugins/user/models';
|
|
|
|
import { AxiosResponse } from 'axios';
|
2020-10-16 11:54:01 +00:00
|
|
|
import { LocalStorage, Loading } from 'quasar';
|
2020-10-16 06:45:40 +00:00
|
|
|
import { Router } from 'src/router';
|
2020-10-15 09:23:41 +00:00
|
|
|
|
2020-10-16 06:45:40 +00:00
|
|
|
export interface UserStateInterface extends LoginResponse {
|
|
|
|
loginLoading: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface LoginResponse {
|
2020-10-15 09:23:41 +00:00
|
|
|
permissions: string[];
|
2020-10-18 23:45:06 +00:00
|
|
|
session: Session;
|
2020-10-15 09:23:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const state: UserStateInterface = {
|
|
|
|
permissions: [],
|
2020-10-18 23:45:06 +00:00
|
|
|
session: {
|
2020-10-19 11:59:56 +00:00
|
|
|
browser: '',
|
|
|
|
expires: new Date(),
|
|
|
|
lifetime: -1,
|
|
|
|
platform: '',
|
|
|
|
token: '',
|
2020-10-18 23:45:06 +00:00
|
|
|
user: {
|
|
|
|
display_name: '',
|
|
|
|
firstname: '',
|
|
|
|
lastname: '',
|
|
|
|
mail: '',
|
|
|
|
roles: [],
|
|
|
|
userid: ''
|
|
|
|
}
|
2020-10-15 09:23:41 +00:00
|
|
|
},
|
2020-10-16 06:45:40 +00:00
|
|
|
loginLoading: false
|
2020-10-15 09:23:41 +00:00
|
|
|
};
|
|
|
|
|
2020-10-16 06:45:40 +00:00
|
|
|
const mutations: MutationTree<UserStateInterface> = {
|
2020-10-19 11:59:56 +00:00
|
|
|
setPermissions(state, data: []) {
|
2020-10-16 06:45:40 +00:00
|
|
|
state.permissions = data;
|
|
|
|
},
|
2020-10-19 11:59:56 +00:00
|
|
|
setSession(state, data: Session) {
|
2020-10-18 23:45:06 +00:00
|
|
|
state.session = data;
|
2020-10-16 06:45:40 +00:00
|
|
|
},
|
2020-10-19 11:59:56 +00:00
|
|
|
setLoginLoading(state, data: boolean) {
|
2020-10-16 06:45:40 +00:00
|
|
|
state.loginLoading = data;
|
|
|
|
},
|
2020-10-19 11:59:56 +00:00
|
|
|
showState(state) {
|
2020-10-16 06:45:40 +00:00
|
|
|
console.log(state);
|
2020-10-15 09:23:41 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const actions: ActionTree<UserStateInterface, StateInterface> = {
|
2020-10-19 11:59:56 +00:00
|
|
|
login({ commit }, data: LoginData) {
|
2020-10-16 06:45:40 +00:00
|
|
|
commit('setLoginLoading', true);
|
2020-10-16 11:54:01 +00:00
|
|
|
Loading.show({ message: 'Du wirst eingeloggt' });
|
2020-10-16 06:45:40 +00:00
|
|
|
void axios
|
2020-10-16 20:37:37 +00:00
|
|
|
.post('/auth', data)
|
2020-10-16 06:45:40 +00:00
|
|
|
.then((response: AxiosResponse<LoginResponse>) => {
|
|
|
|
commit('setPermissions', response.data.permissions);
|
|
|
|
console.log('saved permisisons');
|
2020-10-18 23:45:06 +00:00
|
|
|
commit('setSession', response.data.session);
|
2020-10-16 06:45:40 +00:00
|
|
|
commit('showState');
|
2020-10-16 11:07:31 +00:00
|
|
|
LocalStorage.set('permissions', response.data.permissions);
|
2020-10-18 23:45:06 +00:00
|
|
|
LocalStorage.set('session', response.data.session);
|
2020-10-16 06:45:40 +00:00
|
|
|
|
|
|
|
void Router.push({ name: 'user' });
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.exception(error);
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
commit('setLoginLoading', false);
|
2020-10-16 11:54:01 +00:00
|
|
|
Loading.hide();
|
2020-10-16 06:45:40 +00:00
|
|
|
});
|
2020-10-16 20:37:37 +00:00
|
|
|
},
|
2020-10-19 11:59:56 +00:00
|
|
|
logout({ commit }, token) {
|
|
|
|
void axios.delete(`/auth/${token}`).then(() => {
|
|
|
|
commit('setPermissions', []);
|
|
|
|
commit('setToken', {
|
|
|
|
browser: '',
|
|
|
|
expires: '',
|
|
|
|
lifetime: '',
|
|
|
|
platform: '',
|
|
|
|
token: ''
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
loadFromLocalStorage({ commit }) {
|
2020-10-16 20:37:37 +00:00
|
|
|
console.log('load from store');
|
|
|
|
let data = LocalStorage.getItem('permissions');
|
|
|
|
commit('setPermissions', data ? data : []);
|
2020-10-18 23:45:06 +00:00
|
|
|
data = LocalStorage.getItem('session');
|
2020-10-16 20:37:37 +00:00
|
|
|
commit(
|
2020-10-18 23:45:06 +00:00
|
|
|
'setSession',
|
2020-10-16 20:37:37 +00:00
|
|
|
data
|
|
|
|
? data
|
2020-10-19 11:59:56 +00:00
|
|
|
: {
|
|
|
|
browser: '',
|
|
|
|
expires: new Date(),
|
|
|
|
lifetime: -1,
|
|
|
|
platform: '',
|
|
|
|
token: ''
|
|
|
|
}
|
2020-10-16 20:37:37 +00:00
|
|
|
);
|
|
|
|
commit('showState');
|
2020-10-16 06:45:40 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const getters: GetterTree<UserStateInterface, StateInterface> = {
|
2020-10-19 11:59:56 +00:00
|
|
|
permissions({ permissions }) {
|
2020-10-16 06:45:40 +00:00
|
|
|
return permissions;
|
|
|
|
},
|
2020-10-19 11:59:56 +00:00
|
|
|
session({ session }) {
|
2020-10-18 23:45:06 +00:00
|
|
|
return session;
|
2020-10-16 06:45:40 +00:00
|
|
|
},
|
2020-10-19 11:59:56 +00:00
|
|
|
loginLoading({ loginLoading }) {
|
2020-10-16 06:45:40 +00:00
|
|
|
return loginLoading;
|
2020-10-17 10:56:25 +00:00
|
|
|
},
|
2020-10-19 11:59:56 +00:00
|
|
|
displayName({ session: { user } }) {
|
2020-10-17 10:56:25 +00:00
|
|
|
return `${user.firstname} ${user.lastname}`;
|
2020-10-15 09:23:41 +00:00
|
|
|
}
|
|
|
|
};
|
2020-10-16 06:45:40 +00:00
|
|
|
|
|
|
|
const userStore: Module<UserStateInterface, StateInterface> = {
|
|
|
|
namespaced: true,
|
|
|
|
actions,
|
|
|
|
getters,
|
|
|
|
mutations,
|
|
|
|
state
|
|
|
|
};
|
|
|
|
|
|
|
|
export default userStore;
|