Added login to the application to make authenticated api requests.
This commit is contained in:
@@ -3,6 +3,7 @@ import Vuex from 'vuex'
|
||||
import booksmodule from './modules/books';
|
||||
import bookprogressmodule from './modules/bookprogress';
|
||||
import filemodule from './modules/filemodule';
|
||||
import usermodule from './modules/usermodule';
|
||||
|
||||
Vue.use(Vuex)
|
||||
|
||||
@@ -10,7 +11,8 @@ export default new Vuex.Store({
|
||||
modules:{
|
||||
booksmodule,
|
||||
bookprogressmodule,
|
||||
filemodule
|
||||
filemodule,
|
||||
usermodule
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -8,5 +8,10 @@ export const
|
||||
FETCHING_FILES = "FETCHING_FILES",
|
||||
FETCHING_FILES_SUCCESS = "FETCHING_FILES_SUCCESS",
|
||||
FETCHING_FILES_ERROR = "FETCHING_FILES_ERROR",
|
||||
REMOVE_FILE_FROM_LIST = "REMOVE_FILE_FROM_LIST"
|
||||
REMOVE_FILE_FROM_LIST = "REMOVE_FILE_FROM_LIST",
|
||||
|
||||
LOGIN_START = "LOGIN_START",
|
||||
LOGIN_SUCCESS = "LOGIN_SUCCESS",
|
||||
LOGIN_ERROR = "LOGIN_ERROR",
|
||||
STORE_USER_INFO = "STORE_USER_INFO"
|
||||
;
|
||||
Reference in New Issue
Block a user