User.js 759 B

1
  1. import Logger from'../Logger';import API from'./API';class User{constructor(){this.user={},this._load()}login(a){return API.post('users/login',a,this.user.token).then(a=>(this.user=a,this._save(),a))}logout(){return API.get('users/logout',this.user.token).then(a=>(this.user={},this._save(),a))}register(a){return API.post('users/register',a,this.user.token).then(a=>(this.user=a,this._save(),a))}me(){return this.user}_load(){try{this.user=JSON.parse(localStorage.getItem('user'))}catch(a){Logger.error(a),localStorage.setItem('user',{}),this.user={}}}_save(){try{localStorage.setItem('user',JSON.stringify(this.user))}catch(a){Logger.error(a)}}isLogged(){return null!==this.user&&this.user.token}getName(){return this.user.username}}export default new User;