UNPKG

13.9 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports["default"] = exports.AuthStore = exports.AuthApi = void 0;
9
10var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
11
12var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
13
14var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
15
16var _initializerDefineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/initializerDefineProperty"));
17
18var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
19
20var _applyDecoratedDescriptor2 = _interopRequireDefault(require("@babel/runtime/helpers/applyDecoratedDescriptor"));
21
22var _initializerWarningHelper2 = _interopRequireDefault(require("@babel/runtime/helpers/initializerWarningHelper"));
23
24var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
25
26var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
27
28var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
29
30var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
31
32var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
33
34var _Api2 = require("@lskjs/mobx/mobxStores/Api");
35
36var _Store2 = require("@lskjs/mobx/mobxStores/Store");
37
38var _forEach = _interopRequireDefault(require("lodash/forEach"));
39
40var _mobx = require("mobx");
41
42var _excluded = ["_id"];
43
44var _class, _descriptor, _descriptor2, _class2, _temp;
45
46function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }
47
48function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
49
50var AuthApi = /*#__PURE__*/function (_Api) {
51 (0, _inherits2["default"])(AuthApi, _Api);
52
53 var _super = _createSuper(AuthApi);
54
55 function AuthApi() {
56 var _this;
57
58 (0, _classCallCheck2["default"])(this, AuthApi);
59
60 for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
61 args[_key] = arguments[_key];
62 }
63
64 _this = _super.call.apply(_super, [this].concat(args));
65 _this.baseURL = '/api/auth';
66 return _this;
67 }
68
69 (0, _createClass2["default"])(AuthApi, [{
70 key: "login",
71 value: function login(data) {
72 return this.fetch('/login', {
73 method: 'POST',
74 data: data
75 });
76 }
77 }, {
78 key: "signup",
79 value: function signup(data) {
80 return this.fetch('/signup', {
81 method: 'POST',
82 data: data
83 });
84 }
85 }, {
86 key: "silent",
87 value: function silent(data) {
88 return this.fetch('/silent', {
89 method: 'POST',
90 data: data
91 });
92 }
93 }, {
94 key: "setData",
95 value: function setData(data) {
96 return this.fetch('/setData', {
97 method: 'POST',
98 data: data
99 });
100 }
101 }, {
102 key: "check",
103 value: function check(data) {
104 return this.fetch('/check', {
105 method: 'POST',
106 data: data
107 });
108 }
109 }, {
110 key: "restore",
111 value: function restore(data) {
112 return this.fetch('/restore', {
113 method: 'POST',
114 data: data
115 });
116 }
117 }, {
118 key: "status",
119 value: function status(data) {
120 return this.fetch('/status', {
121 method: 'POST',
122 data: data
123 });
124 }
125 }, {
126 key: "session",
127 value: function session(data) {
128 return this.fetch('/session', {
129 method: 'POST',
130 data: data
131 });
132 }
133 }, {
134 key: "info",
135 value: function info(data) {
136 return this.fetch('/info', {
137 method: 'POST',
138 data: data
139 });
140 } // async getUser(body) {
141 // const res = await this.fetch('/api/module/user/get', {
142 // method: 'POST',
143 // body,
144 // });
145 // return res.data;
146 // }
147 // async getMyUser(body) {
148 // // }
149 // const res = await this.fetch('/api/user/me', {
150 // method: 'POST',
151 // body,
152 // });
153 // return res.data;
154 // }
155 // async userEdit(body) {
156 // const res = await this.fetch('/api/module/user/edit', {
157 // method: 'POST',
158 // body,
159 // qs: { _id: body._id },
160 // });
161 // return res;
162 // }
163 // async loginPassport(data) {
164 // const res = await this.fetch('/social'login`, {
165 // method: 'POST',
166 // data,
167 // });
168 // return res.data;
169 // }
170
171 }]);
172 return AuthApi;
173}(_Api2.Api);
174
175exports.AuthApi = AuthApi;
176var AuthStore = (_class = (_temp = _class2 = /*#__PURE__*/function (_Store) {
177 (0, _inherits2["default"])(AuthStore, _Store);
178
179 var _super2 = _createSuper(AuthStore);
180
181 function AuthStore() {
182 var _this2;
183
184 (0, _classCallCheck2["default"])(this, AuthStore);
185
186 for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
187 args[_key2] = arguments[_key2];
188 }
189
190 _this2 = _super2.call.apply(_super2, [this].concat(args));
191 (0, _initializerDefineProperty2["default"])(_this2, "session", _descriptor, (0, _assertThisInitialized2["default"])(_this2));
192 (0, _initializerDefineProperty2["default"])(_this2, "sessions", _descriptor2, (0, _assertThisInitialized2["default"])(_this2));
193 return _this2;
194 }
195
196 (0, _createClass2["default"])(AuthStore, [{
197 key: "applySession",
198 value: function () {
199 var _applySession = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(_ref) {
200 var _id, props, app, session;
201
202 return _regenerator["default"].wrap(function _callee$(_context) {
203 while (1) {
204 switch (_context.prev = _context.next) {
205 case 0:
206 _id = _ref._id, props = (0, _objectWithoutProperties2["default"])(_ref, _excluded);
207 app = this.constructor.app;
208
209 if (_id) {
210 _context.next = 5;
211 break;
212 }
213
214 // eslint-disable-next-line no-console
215 app.log.warn('AuthStore.applySession | "session._id" not provided!');
216 return _context.abrupt("return");
217
218 case 5:
219 session = this.sessions.filter(function (s) {
220 return s._id === _id;
221 })[0];
222
223 if (!session) {
224 session = {
225 _id: _id
226 };
227 this.sessions.push(session);
228 }
229
230 (0, _forEach["default"])(props, function (value, key) {
231 session[key] = value;
232 });
233 this.session = session;
234
235 case 9:
236 case "end":
237 return _context.stop();
238 }
239 }
240 }, _callee, this);
241 }));
242
243 function applySession(_x) {
244 return _applySession.apply(this, arguments);
245 }
246
247 return applySession;
248 }()
249 }, {
250 key: "getSession",
251 value: function getSession() {
252 return this.session;
253 }
254 }, {
255 key: "getUserId",
256 value: function getUserId() {
257 var session = this.getSession();
258 return session ? session._id : null;
259 }
260 }, {
261 key: "isAuth",
262 value: function isAuth() {
263 return !!this.getUserId();
264 }
265 }, {
266 key: "login",
267 value: function () {
268 var _login = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(props) {
269 var _yield$this$construct, session;
270
271 return _regenerator["default"].wrap(function _callee2$(_context2) {
272 while (1) {
273 switch (_context2.prev = _context2.next) {
274 case 0:
275 _context2.next = 2;
276 return this.constructor.api.login(props);
277
278 case 2:
279 _yield$this$construct = _context2.sent;
280 session = _yield$this$construct.data;
281 this.applySession(session);
282 return _context2.abrupt("return", session);
283
284 case 6:
285 case "end":
286 return _context2.stop();
287 }
288 }
289 }, _callee2, this);
290 }));
291
292 function login(_x2) {
293 return _login.apply(this, arguments);
294 }
295
296 return login;
297 }()
298 }, {
299 key: "signup",
300 value: function () {
301 var _signup = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3(props) {
302 var _yield$this$construct2, session;
303
304 return _regenerator["default"].wrap(function _callee3$(_context3) {
305 while (1) {
306 switch (_context3.prev = _context3.next) {
307 case 0:
308 _context3.next = 2;
309 return this.constructor.api.signup(props);
310
311 case 2:
312 _yield$this$construct2 = _context3.sent;
313 session = _yield$this$construct2.data;
314 this.applySession(session);
315 return _context3.abrupt("return", session);
316
317 case 6:
318 case "end":
319 return _context3.stop();
320 }
321 }
322 }, _callee3, this);
323 }));
324
325 function signup(_x3) {
326 return _signup.apply(this, arguments);
327 }
328
329 return signup;
330 }()
331 }, {
332 key: "updateSession",
333 value: function () {
334 var _updateSession = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4(props) {
335 var _yield$this$construct3, session;
336
337 return _regenerator["default"].wrap(function _callee4$(_context4) {
338 while (1) {
339 switch (_context4.prev = _context4.next) {
340 case 0:
341 _context4.next = 2;
342 return this.constructor.api.session(props);
343
344 case 2:
345 _yield$this$construct3 = _context4.sent;
346 session = _yield$this$construct3.data;
347 this.applySession(session);
348 return _context4.abrupt("return", session);
349
350 case 6:
351 case "end":
352 return _context4.stop();
353 }
354 }
355 }, _callee4, this);
356 }));
357
358 function updateSession(_x4) {
359 return _updateSession.apply(this, arguments);
360 }
361
362 return updateSession;
363 }()
364 }, {
365 key: "logout",
366 value: function () {
367 var _logout = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee5() {
368 var _this3 = this;
369
370 return _regenerator["default"].wrap(function _callee5$(_context5) {
371 while (1) {
372 switch (_context5.prev = _context5.next) {
373 case 0:
374 this.sessions = this.sessions.filter(function (s) {
375 return s._id !== _this3.session._id;
376 }); // eslint-disable-next-line prefer-destructuring
377
378 this.session = this.sessions[0];
379
380 case 2:
381 case "end":
382 return _context5.stop();
383 }
384 }
385 }, _callee5, this);
386 }));
387
388 function logout() {
389 return _logout.apply(this, arguments);
390 }
391
392 return logout;
393 }()
394 }, {
395 key: "getProviders",
396 value: function () {
397 var _getProviders = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee6() {
398 var _yield$this$construct4, data;
399
400 return _regenerator["default"].wrap(function _callee6$(_context6) {
401 while (1) {
402 switch (_context6.prev = _context6.next) {
403 case 0:
404 _context6.next = 2;
405 return this.constructor.api.getInfo();
406
407 case 2:
408 _yield$this$construct4 = _context6.sent;
409 data = _yield$this$construct4.data;
410 return _context6.abrupt("return", data.providers || []);
411
412 case 5:
413 case "end":
414 return _context6.stop();
415 }
416 }
417 }, _callee6, this);
418 }));
419
420 function getProviders() {
421 return _getProviders.apply(this, arguments);
422 }
423
424 return getProviders;
425 }()
426 }]);
427 return AuthStore;
428}(_Store2.Store), _class2.Api = AuthApi, _temp), (_descriptor = (0, _applyDecoratedDescriptor2["default"])(_class.prototype, "session", [_mobx.observable], {
429 configurable: true,
430 enumerable: true,
431 writable: true,
432 initializer: function initializer() {
433 return null;
434 }
435}), _descriptor2 = (0, _applyDecoratedDescriptor2["default"])(_class.prototype, "sessions", [_mobx.observable], {
436 configurable: true,
437 enumerable: true,
438 writable: true,
439 initializer: function initializer() {
440 return [];
441 }
442}), (0, _applyDecoratedDescriptor2["default"])(_class.prototype, "applySession", [_mobx.action], Object.getOwnPropertyDescriptor(_class.prototype, "applySession"), _class.prototype)), _class);
443exports.AuthStore = AuthStore;
444var _default = AuthStore;
445exports["default"] = _default;
446//# sourceMappingURL=AuthStore.js.map
\No newline at end of file