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-21 15:39:04 +00:00
|
|
|
user: FG.User;
|
|
|
|
session: FG.Session;
|
2020-10-15 09:23:41 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 23:19:39 +00:00
|
|
|
const empty_session: FG.Session = {
|
|
|
|
browser: '',
|
|
|
|
expires: new Date(),
|
|
|
|
lifetime: -1,
|
|
|
|
platform: '',
|
|
|
|
token: ''
|
|
|
|
};
|
|
|
|
|
|
|
|
const empty_user: FG.User = {
|
2020-10-19 14:49:40 +00:00
|
|
|
display_name: '',
|
|
|
|
firstname: '',
|
|
|
|
lastname: '',
|
|
|
|
mail: '',
|
|
|
|
roles: [],
|
|
|
|
userid: ''
|
2020-10-28 23:19:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const state: UserStateInterface = {
|
|
|
|
user: empty_user,
|
|
|
|
session: empty_session,
|
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-27 12:49:45 +00:00
|
|
|
setUser(state, data: FG.User) {
|
2020-10-19 14:49:40 +00:00
|
|
|
state.user = data;
|
2020-10-16 06:45:40 +00:00
|
|
|
},
|
2020-10-27 12:49:45 +00:00
|
|
|
setSession(state, data: FG.Session) {
|
2020-10-18 23:45:06 +00:00
|
|
|
state.session = data;
|
2020-10-16 06:45:40 +00:00
|
|
|
},
|
2020-10-27 12:49:45 +00:00
|
|
|
setLoginLoading(state, data: boolean) {
|
2020-10-16 06:45:40 +00:00
|
|
|
state.loginLoading = data;
|
|
|
|
},
|
2020-10-27 12:49:45 +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-27 12:49:45 +00:00
|
|
|
login({ commit }, data: LoginData) {
|
2020-10-16 06:45:40 +00:00
|
|
|
commit('setLoginLoading', true);
|
2020-10-28 11:55:20 +00:00
|
|
|
Loading.setDefaults({
|
|
|
|
spinner: () => import('src/components/loading/DarkCircularProgress.vue')
|
|
|
|
});
|
|
|
|
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>) => {
|
2020-10-28 23:19:39 +00:00
|
|
|
response.data.session.expires = new Date(response.data.session.expires);
|
2020-10-19 14:49:40 +00:00
|
|
|
commit('setUser', response.data.user);
|
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-19 14:49:40 +00:00
|
|
|
LocalStorage.set('user', response.data.user);
|
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-28 23:19:39 +00:00
|
|
|
|
|
|
|
doLogout({commit}, token: string) {
|
2020-10-28 11:55:20 +00:00
|
|
|
Loading.show({ message: 'Du wirst ausgeloggt' });
|
|
|
|
void axios
|
|
|
|
.delete(`/auth/${token}`)
|
|
|
|
.then(() => {
|
2020-10-28 23:19:39 +00:00
|
|
|
commit('setUser', empty_user);
|
|
|
|
commit('setSession', empty_session);
|
2020-10-28 11:55:20 +00:00
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
LocalStorage.remove('user');
|
|
|
|
LocalStorage.remove('session');
|
|
|
|
Loading.hide();
|
2020-10-19 11:59:56 +00:00
|
|
|
});
|
|
|
|
},
|
2020-10-28 23:19:39 +00:00
|
|
|
|
|
|
|
logout({ dispatch}, token: string) {
|
|
|
|
dispatch('doLogout', token).finally(() => {void Router.push({ name: 'login' });});
|
|
|
|
},
|
|
|
|
|
2020-10-27 12:49:45 +00:00
|
|
|
updateUser({ commit, getters }, data) {
|
|
|
|
commit('setLoginLoading', true);
|
|
|
|
axios
|
|
|
|
.put(`/users/${getters.user.userid}`, data)
|
|
|
|
.catch(error => {
|
|
|
|
console.log(error);
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
commit('setLoginLoading', false);
|
|
|
|
});
|
|
|
|
},
|
2020-10-28 23:19:39 +00:00
|
|
|
|
2020-10-27 12:49:45 +00:00
|
|
|
loadFromLocalStorage({ commit }) {
|
2020-10-16 20:37:37 +00:00
|
|
|
console.log('load from store');
|
2020-10-19 14:49:40 +00:00
|
|
|
let data = LocalStorage.getItem('user');
|
2020-10-27 12:49:45 +00:00
|
|
|
commit(
|
|
|
|
'setUser',
|
|
|
|
data
|
|
|
|
? data
|
2020-10-28 23:19:39 +00:00
|
|
|
: empty_user
|
2020-10-27 12:49:45 +00:00
|
|
|
);
|
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-28 23:19:39 +00:00
|
|
|
: empty_session
|
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-27 12:49:45 +00:00
|
|
|
user({ user }) {
|
2020-10-19 14:49:40 +00:00
|
|
|
return user;
|
2020-10-16 06:45:40 +00:00
|
|
|
},
|
2020-10-27 12:49:45 +00:00
|
|
|
displayName({ user }) {
|
2020-10-20 17:24:37 +00:00
|
|
|
return user.display_name;
|
|
|
|
},
|
2020-10-27 12:49:45 +00:00
|
|
|
session({ session }) {
|
2020-10-18 23:45:06 +00:00
|
|
|
return session;
|
2020-10-16 06:45:40 +00:00
|
|
|
},
|
2020-10-27 12:49:45 +00:00
|
|
|
loginLoading({ loginLoading }) {
|
2020-10-16 06:45:40 +00:00
|
|
|
return loginLoading;
|
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;
|