Files
biblio/assets/js/store/modules/usermodule.js

83 lines
2.4 KiB
JavaScript

import UserApi from "../../api/user";
import {
LOGIN_START,
LOGIN_SUCCESS,
LOGIN_ERROR,
STORE_USER_INFO,
LOGIN_STOP,
} from '../mutation-types.js'
export default {
namespaced: true,
state: {
isLoading: false,
error: null,
user: window.user,
userUri: null,
},
getters: {
isLoading(state) {
return state.isLoading;
},
hasError(state) {
return state.error !== null;
},
error(state) {
return state.error;
}
},
mutations: {
[LOGIN_START](state) {
state.isLoading = true;
state.error = null;
state.user = null;
},
[LOGIN_SUCCESS](state, userUri) {
state.isLoading = false;
state.error = null;
state.userUri = userUri;
},
[LOGIN_STOP](state, userUri) {
state.isLoading = false;
},
[STORE_USER_INFO](state, user) {
state.isLoading = false;
state.error = null;
state.user = user;
},
[LOGIN_ERROR](state, error) {
state.isLoading = false;
state.error = error;
state.user = null;
},
},
actions: {
async login({dispatch, commit}, data) {
console.info(data.email);
console.info(data.password);
commit(LOGIN_START);
let response = await UserApi.login(data.email, data.password)
.then(response => {
console.log(response.data);
console.log(response.headers.location);
// commit(LOGIN_SUCCESS,response.data);
dispatch('getUserInfo', response.headers.location)
commit(LOGIN_SUCCESS, response.headers.location);
//this.$emit('user-authenticated', userUri);
}).catch(error => {
if (error.response.data.error) {
commit(LOGIN_ERROR, error.response.data.error);
console.log(error.response.data.error);
}
}).finally(() => {
commit(LOGIN_STOP);
})
},
async getUserInfo({commit}, userUri) {
let userInfo = await UserApi.getUserData(userUri);
commit(STORE_USER_INFO, userInfo.data);
}
}
};