Handle state mutations for users better in Vuex
This commit is contained in:
parent
a1444b047e
commit
caa0d21ac9
2 changed files with 55 additions and 30 deletions
|
@ -1,4 +1,4 @@
|
|||
import User from './models/user';
|
||||
import { User } from './models/user';
|
||||
|
||||
export const storeData = {
|
||||
state: {
|
||||
|
@ -13,15 +13,25 @@ export const storeData = {
|
|||
},
|
||||
},
|
||||
getters: {
|
||||
user: function (state) {
|
||||
getCurrentUser: function (state) {
|
||||
if (!(state.user instanceof User)) {
|
||||
state.user = User.fromJWT(localStorage.getItem('token'));
|
||||
}
|
||||
|
||||
return state.user;
|
||||
},
|
||||
},
|
||||
mutations: {
|
||||
/**
|
||||
* Log in a user and store them in vuex using the local storage token.
|
||||
*
|
||||
* @param state
|
||||
*/
|
||||
login: function (state) {
|
||||
state.user = new User().fromJwt(localStorage.getItem('token'));
|
||||
state.user = User.fromJWT(localStorage.getItem('token'));
|
||||
},
|
||||
logout: function (state) {
|
||||
console.log('logout');
|
||||
state.user = null;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue