File tree Expand file tree Collapse file tree 2 files changed +55
-30
lines changed
Expand file tree Collapse file tree 2 files changed +55
-30
lines changed Original file line number Diff line number Diff line change 1+ import { Collection , Model } from 'vue-mc' ;
12import JwtDecode from 'jwt-decode' ;
23
3- const User = function ( ) {
4- this . id = 0 ;
5- this . admin = false ;
6- this . email = '' ;
7- } ;
4+ export class User extends Model {
5+ static defaults ( ) {
6+ return {
7+ id : null ,
8+ uuid : '' ,
9+ username : '' ,
10+ email : '' ,
11+ name_first : '' ,
12+ name_last : '' ,
13+ language : 'en' ,
14+ root_admin : false ,
15+ }
16+ }
817
9- /**
10- * Return a new instance of the user model using a JWT.
11- *
12- * @param {string } token
13- * @returns {User }
14- */
15- User . prototype . fromJwt = function ( token ) {
16- return this . newModel ( JwtDecode ( token ) ) ;
17- } ;
18+ static mutations ( ) {
19+ return {
20+ id : Number ,
21+ uuid : String ,
22+ username : String ,
23+ email : String ,
24+ name_first : String ,
25+ name_last : String ,
26+ language : String ,
27+ root_admin : Boolean ,
28+ }
29+ }
1830
19- /**
20- * Return an instance of this user model with the properties set on it.
21- *
22- * @param {object } obj
23- * @returns {User }
24- */
25- User . prototype . newModel = function ( obj ) {
26- this . id = obj . id ;
27- this . admin = obj . admin ;
28- this . email = obj . email ;
31+ static fromJWT ( token ) {
32+ return new User ( JwtDecode ( token ) . user || { } ) ;
33+ }
34+ }
2935
30- return this ;
31- } ;
36+ export class UserCollection extends Collection {
37+ static model ( ) {
38+ return User ;
39+ }
3240
33- export default User ;
41+ get todo ( ) {
42+ return this . sum ( 'done' ) ;
43+ }
44+
45+ get done ( ) {
46+ return this . todo === 0 ;
47+ }
48+ }
Original file line number Diff line number Diff line change 1- import User from './models/user' ;
1+ import { User } from './models/user' ;
22
33export const storeData = {
44 state : {
@@ -13,15 +13,25 @@ export const storeData = {
1313 } ,
1414 } ,
1515 getters : {
16- user : function ( state ) {
16+ getCurrentUser : function ( state ) {
17+ if ( ! ( state . user instanceof User ) ) {
18+ state . user = User . fromJWT ( localStorage . getItem ( 'token' ) ) ;
19+ }
20+
1721 return state . user ;
1822 } ,
1923 } ,
2024 mutations : {
25+ /**
26+ * Log in a user and store them in vuex using the local storage token.
27+ *
28+ * @param state
29+ */
2130 login : function ( state ) {
22- state . user = new User ( ) . fromJwt ( localStorage . getItem ( 'token' ) ) ;
31+ state . user = User . fromJWT ( localStorage . getItem ( 'token' ) ) ;
2332 } ,
2433 logout : function ( state ) {
34+ console . log ( 'logout' ) ;
2535 state . user = null ;
2636 }
2737 }
You can’t perform that action at this time.
0 commit comments