Added login to the application to make authenticated api requests.
This commit is contained in:
81
assets/js/store/modules/usermodule.js
Normal file
81
assets/js/store/modules/usermodule.js
Normal file
@@ -0,0 +1,81 @@
|
||||
import UserApi from "../../api/user";
|
||||
import {
|
||||
LOGIN_START,
|
||||
LOGIN_SUCCESS,
|
||||
LOGIN_ERROR,
|
||||
STORE_USER_INFO,
|
||||
} from '../mutation-types.js'
|
||||
|
||||
export default {
|
||||
namespaced: true,
|
||||
state: {
|
||||
isLoading: false,
|
||||
error: null,
|
||||
user: null,
|
||||
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;
|
||||
},
|
||||
[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);
|
||||
//this.email = '';
|
||||
//this.password = '';
|
||||
}).catch(error => {
|
||||
if (error.response.data.error) {
|
||||
|
||||
commit(LOGIN_ERROR, error.response.data.error);
|
||||
console.log(error.response.data.error);
|
||||
}
|
||||
|
||||
}).finally(() => {
|
||||
// this.isLoading = false;
|
||||
})
|
||||
},
|
||||
async getUserInfo({commit}, userUri) {
|
||||
let userInfo = await UserApi.getUserData(userUri);
|
||||
commit(STORE_USER_INFO, userInfo.data);
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user