forked from jonashackt/spring-boot-vuejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.js
59 lines (57 loc) · 2.01 KB
/
store.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import Vue from 'vue'
import Vuex from 'vuex'
import api from './components/backend-api'
Vue.use(Vuex);
export default new Vuex.Store({
state: {
loginSuccess: false,
loginError: false,
userName: null,
userPass: null
},
mutations: {
login_success(state, payload){
state.loginSuccess = true;
state.userName = payload.userName;
state.userPass = payload.userPass;
},
login_error(state, payload){
state.loginError = true;
state.userName = payload.userName;
}
},
actions: {
login({commit}, {user, password}) {
return new Promise((resolve, reject) => {
console.log("Accessing backend with user: '" + user);
api.getSecured(user, password)
.then(response => {
console.log("Response: '" + response.data + "' with Statuscode " + response.status);
if(response.status == 200) {
console.log("Login successful");
// place the loginSuccess state into our vuex store
commit('login_success', {
userName: user,
userPass: password
});
}
resolve(response)
})
.catch(error => {
console.log("Error: " + error);
// place the loginError state into our vuex store
commit('login_error', {
userName: user
});
reject("Invalid credentials!")
})
})
}
},
getters: {
isLoggedIn: state => state.loginSuccess,
hasLoginErrored: state => state.loginError,
getUserName: state => state.userName,
getUserPass: state => state.userPass
}
})