123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.default = void 0;
- var _Logger = _interopRequireDefault(require("../Logger"));
- var _API = _interopRequireDefault(require("./API"));
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
- function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
- var User =
- /*#__PURE__*/
- function () {
- function User() {
- _classCallCheck(this, User);
- this.user = {};
- this._load();
- }
- _createClass(User, [{
- key: "login",
- value: function login(data) {
- var _this = this;
- return _API.default.post('users/login', data, this.user.token).then(function (user) {
- _this.user = user;
- _this._save();
- return user;
- });
- }
- }, {
- key: "logout",
- value: function logout(data) {
- var _this2 = this;
- return _API.default.get('users/logout', this.user.token).then(function (user) {
- _this2.user = {};
- _this2._save();
- return user;
- });
- }
- }, {
- key: "register",
- value: function register(data) {
- var _this3 = this;
- return _API.default.post('users/register', data, this.user.token).then(function (user) {
- _this3.user = user;
- _this3._save();
- return user;
- });
- }
- }, {
- key: "me",
- value: function me() {
- return this.user;
- }
- }, {
- key: "_load",
- value: function _load() {
- try {
- this.user = JSON.parse(localStorage.getItem('user'));
- } catch (e) {
- _Logger.default.error(e);
- localStorage.setItem('user', {});
- this.user = {};
- }
- }
- }, {
- key: "_save",
- value: function _save() {
- try {
- localStorage.setItem('user', JSON.stringify(this.user));
- } catch (e) {
- _Logger.default.error(e);
- }
- }
- }, {
- key: "isLogged",
- value: function isLogged() {
- return this.user !== null && this.user.token;
- }
- }, {
- key: "getName",
- value: function getName() {
- return this.user.username;
- }
- }]);
- return User;
- }();
- var _default = new User();
- exports.default = _default;
|