UserService.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _react = _interopRequireDefault(require("react"));
  7. var _modularityFront = _interopRequireDefault(require("modularity-front"));
  8. var _reactMaterialize = require("react-materialize");
  9. var _API = _interopRequireDefault(require("./API"));
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  12. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  13. 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); } }
  14. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  15. function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
  16. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  17. function _get(target, property, receiver) { if (typeof Reflect !== "undefined" && Reflect.get) { _get = Reflect.get; } else { _get = function _get(target, property, receiver) { var base = _superPropBase(target, property); if (!base) return; var desc = Object.getOwnPropertyDescriptor(base, property); if (desc.get) { return desc.get.call(receiver); } return desc.value; }; } return _get(target, property, receiver || target); }
  18. function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; }
  19. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  20. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
  21. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  22. var UserService =
  23. /*#__PURE__*/
  24. function (_Modularity$Service) {
  25. _inherits(UserService, _Modularity$Service);
  26. function UserService(props) {
  27. var _this;
  28. _classCallCheck(this, UserService);
  29. _this = _possibleConstructorReturn(this, _getPrototypeOf(UserService).call(this, props));
  30. _this.state = {
  31. username: '',
  32. password: ''
  33. };
  34. _this.API = new _API.default('/users', UserService.Manager.getToken());
  35. _this.getUser(UserService.Manager.getToken());
  36. return _this;
  37. }
  38. _createClass(UserService, [{
  39. key: "getToken",
  40. value: function getToken() {
  41. return UserService.Manager.getToken();
  42. }
  43. }, {
  44. key: "isLogged",
  45. value: function isLogged() {
  46. var token = UserService.Manager.getToken();
  47. return !!token && token !== "undefined" && token !== 'missing';
  48. }
  49. }, {
  50. key: "logout",
  51. value: function logout() {
  52. var _this2 = this;
  53. return this.API.get('/logout').then(function () {
  54. _this2.User.set({});
  55. _this2.Token.set('missing');
  56. UserService.Manager.removeToken();
  57. });
  58. }
  59. }, {
  60. key: "login",
  61. value: function login() {
  62. var _this3 = this;
  63. var inputdata = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.state;
  64. var username = inputdata.username,
  65. password = inputdata.password;
  66. return this.API.post('/login', {
  67. username: username,
  68. password: password
  69. }).then(function (data) {
  70. return _this3.getUser(data.token);
  71. });
  72. }
  73. }, {
  74. key: "register",
  75. value: function register() {
  76. var _this4 = this;
  77. var inputdata = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.state;
  78. var email = inputdata.email,
  79. username = inputdata.username,
  80. password = inputdata.password;
  81. return this.API.post('/register', {
  82. email: email,
  83. username: username,
  84. password: password
  85. }).then(function (data) {
  86. return _this4.getUser(data.token);
  87. });
  88. }
  89. }, {
  90. key: "getUser",
  91. value: function getUser(token) {
  92. var _this5 = this;
  93. this.API.setToken(token);
  94. return this.API.get('/me').then(function (data) {
  95. _this5.User.set(data);
  96. _this5.Token.set(data.token || 'missing');
  97. UserService.Manager.setToken(data.token || 'missing');
  98. return data;
  99. }).catch(function () {
  100. _this5.User.set({});
  101. _this5.Token.set('missing');
  102. UserService.Manager.removeToken();
  103. });
  104. }
  105. }, {
  106. key: "confirmUser",
  107. value: function confirmUser(token) {
  108. return this.API.get("/verifyemail?id=" + token);
  109. }
  110. }, {
  111. key: "changePass",
  112. value: function changePass(token, cpass, npass) {
  113. this.API.setToken(token);
  114. return this.API.post('/changepass', {
  115. cpass: cpass,
  116. npass: npass
  117. }).then(function (data) {
  118. return data;
  119. });
  120. }
  121. }, {
  122. key: "render",
  123. value: function render() {
  124. var _this6 = this;
  125. return _react.default.createElement("div", {
  126. className: "UserServiceInfo"
  127. }, "ID : ", this.__renderID(), _react.default.createElement("div", null, _react.default.createElement(_reactMaterialize.Input, {
  128. type: "email",
  129. label: 'Email',
  130. value: this.state.email,
  131. onChange: function onChange(e) {
  132. return _this6.setState({
  133. email: e.target.value
  134. });
  135. }
  136. }), _react.default.createElement(_reactMaterialize.Input, {
  137. type: "text",
  138. label: 'Username',
  139. value: this.state.username,
  140. onChange: function onChange(e) {
  141. return _this6.setState({
  142. username: e.target.value
  143. });
  144. }
  145. }), _react.default.createElement(_reactMaterialize.Input, {
  146. type: "password",
  147. label: 'Password',
  148. value: this.state.password,
  149. onChange: function onChange(e) {
  150. return _this6.setState({
  151. password: e.target.value
  152. });
  153. }
  154. }), _react.default.createElement(_reactMaterialize.Button, {
  155. onClick: function onClick() {
  156. return _this6.login();
  157. }
  158. }, "Login"), _react.default.createElement(_reactMaterialize.Button, {
  159. onClick: function onClick() {
  160. return _this6.register();
  161. }
  162. }, "Register")), _react.default.createElement("div", null, "Token : ", this.Token.value), _react.default.createElement("div", null, this.__factoryOutput(this.User), this.__factoryOutput(this.Token)), _react.default.createElement("div", null, _get(_getPrototypeOf(UserService.prototype), "render", this).call(this)));
  163. }
  164. }]);
  165. return UserService;
  166. }(_modularityFront.default.Service);
  167. var Manager =
  168. /*#__PURE__*/
  169. function () {
  170. function Manager() {
  171. _classCallCheck(this, Manager);
  172. this.token = localStorage.getItem('Token');
  173. }
  174. _createClass(Manager, [{
  175. key: "setToken",
  176. value: function setToken(token) {
  177. this.token = token;
  178. localStorage.setItem('Token', token);
  179. }
  180. }, {
  181. key: "getToken",
  182. value: function getToken() {
  183. return this.token;
  184. }
  185. }, {
  186. key: "removeToken",
  187. value: function removeToken() {
  188. this.token = 'missing';
  189. localStorage.setItem('Token', 'missing');
  190. }
  191. }]);
  192. return Manager;
  193. }();
  194. UserService.Inputs = {};
  195. UserService.Outputs = {
  196. User: {},
  197. Token: {}
  198. };
  199. UserService.Category = "Services";
  200. UserService.Manager = new Manager();
  201. var _default = UserService;
  202. exports.default = _default;