User.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _Logger = _interopRequireDefault(require("../Logger"));
  7. var _API = _interopRequireDefault(require("./API"));
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  10. 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); } }
  11. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  12. var User =
  13. /*#__PURE__*/
  14. function () {
  15. function User() {
  16. _classCallCheck(this, User);
  17. this.user = {};
  18. this._load();
  19. }
  20. _createClass(User, [{
  21. key: "login",
  22. value: function login(data) {
  23. var _this = this;
  24. return _API.default.post('users/login', data, this.user.token).then(function (user) {
  25. _this.user = user;
  26. _this._save();
  27. return user;
  28. });
  29. }
  30. }, {
  31. key: "logout",
  32. value: function logout(data) {
  33. var _this2 = this;
  34. return _API.default.get('users/logout', this.user.token).then(function (user) {
  35. _this2.user = {};
  36. _this2._save();
  37. return user;
  38. });
  39. }
  40. }, {
  41. key: "register",
  42. value: function register(data) {
  43. var _this3 = this;
  44. return _API.default.post('users/register', data, this.user.token).then(function (user) {
  45. _this3.user = user;
  46. _this3._save();
  47. return user;
  48. });
  49. }
  50. }, {
  51. key: "me",
  52. value: function me() {
  53. return this.user;
  54. }
  55. }, {
  56. key: "_load",
  57. value: function _load() {
  58. try {
  59. this.user = JSON.parse(localStorage.getItem('user'));
  60. } catch (e) {
  61. _Logger.default.error(e);
  62. localStorage.setItem('user', {});
  63. this.user = {};
  64. }
  65. }
  66. }, {
  67. key: "_save",
  68. value: function _save() {
  69. try {
  70. localStorage.setItem('user', JSON.stringify(this.user));
  71. } catch (e) {
  72. _Logger.default.error(e);
  73. }
  74. }
  75. }, {
  76. key: "isLogged",
  77. value: function isLogged() {
  78. return this.user !== null && this.user.token;
  79. }
  80. }, {
  81. key: "getName",
  82. value: function getName() {
  83. return this.user.username;
  84. }
  85. }]);
  86. return User;
  87. }();
  88. var _default = new User();
  89. exports.default = _default;