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,33 +1,48 @@
|
||||||
|
import { Collection, Model } from 'vue-mc';
|
||||||
import JwtDecode from 'jwt-decode';
|
import JwtDecode from 'jwt-decode';
|
||||||
|
|
||||||
const User = function () {
|
export class User extends Model {
|
||||||
this.id = 0;
|
static defaults() {
|
||||||
this.admin = false;
|
return {
|
||||||
this.email = '';
|
id: null,
|
||||||
};
|
uuid: '',
|
||||||
|
username: '',
|
||||||
|
email: '',
|
||||||
|
name_first: '',
|
||||||
|
name_last: '',
|
||||||
|
language: 'en',
|
||||||
|
root_admin: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
static mutations() {
|
||||||
* Return a new instance of the user model using a JWT.
|
return {
|
||||||
*
|
id: Number,
|
||||||
* @param {string} token
|
uuid: String,
|
||||||
* @returns {User}
|
username: String,
|
||||||
*/
|
email: String,
|
||||||
User.prototype.fromJwt = function (token) {
|
name_first: String,
|
||||||
return this.newModel(JwtDecode(token));
|
name_last: String,
|
||||||
};
|
language: String,
|
||||||
|
root_admin: Boolean,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
static fromJWT(token) {
|
||||||
* Return an instance of this user model with the properties set on it.
|
return new User(JwtDecode(token).user || {});
|
||||||
*
|
}
|
||||||
* @param {object} obj
|
}
|
||||||
* @returns {User}
|
|
||||||
*/
|
|
||||||
User.prototype.newModel = function (obj) {
|
|
||||||
this.id = obj.id;
|
|
||||||
this.admin = obj.admin;
|
|
||||||
this.email = obj.email;
|
|
||||||
|
|
||||||
return this;
|
export class UserCollection extends Collection {
|
||||||
};
|
static model() {
|
||||||
|
return User;
|
||||||
|
}
|
||||||
|
|
||||||
export default User;
|
get todo() {
|
||||||
|
return this.sum('done');
|
||||||
|
}
|
||||||
|
|
||||||
|
get done() {
|
||||||
|
return this.todo === 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import User from './models/user';
|
import { User } from './models/user';
|
||||||
|
|
||||||
export const storeData = {
|
export const storeData = {
|
||||||
state: {
|
state: {
|
||||||
|
@ -13,15 +13,25 @@ export const storeData = {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
getters: {
|
getters: {
|
||||||
user: function (state) {
|
getCurrentUser: function (state) {
|
||||||
|
if (!(state.user instanceof User)) {
|
||||||
|
state.user = User.fromJWT(localStorage.getItem('token'));
|
||||||
|
}
|
||||||
|
|
||||||
return state.user;
|
return state.user;
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
mutations: {
|
mutations: {
|
||||||
|
/**
|
||||||
|
* Log in a user and store them in vuex using the local storage token.
|
||||||
|
*
|
||||||
|
* @param state
|
||||||
|
*/
|
||||||
login: function (state) {
|
login: function (state) {
|
||||||
state.user = new User().fromJwt(localStorage.getItem('token'));
|
state.user = User.fromJWT(localStorage.getItem('token'));
|
||||||
},
|
},
|
||||||
logout: function (state) {
|
logout: function (state) {
|
||||||
|
console.log('logout');
|
||||||
state.user = null;
|
state.user = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue