UNPKG

redux-security

Version:
555 lines (448 loc) 16.1 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.saga = exports.configSaga = undefined; var _reduxJournal = require('redux-journal'); var _reduxManager = require('redux-manager'); var _effects = require('redux-saga/effects'); var _actions = require('./actions'); var _config = require('./config'); var _select = require('./select'); var tags = _config.TAGS + '.saga'; var configSaga = exports.configSaga = function configSaga() { var _marked = [codeEmail, watchCodeEmail, codeConfirm, watchCodeConfirm, login, watchLogin, logout, watchLogout, signup, watchSignup, docsMaxOverRemove, watchInsert, root].map(regeneratorRuntime.mark); var api = function api(serviceName) { var API = _reduxManager.manager.api.get(serviceName); if (!API) { var ERROR = 'manager.api.get(\'' + serviceName + '\') == undefined'; (0, _reduxJournal.error)(ERROR, tags + '.configSaga.api'); throw new Error(ERROR); } return API; }; function codeEmail(action) { var __ns__; return regeneratorRuntime.wrap(function codeEmail$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: __ns__ = action.__ns__; _context.prev = 1; _context.next = 4; return (0, _effects.put)({ __ns__: __ns__, type: _actions.types.CODE_EMAIL_REQUEST }); case 4: _context.next = 6; return (0, _effects.call)(api(__ns__).codeEmail, action.payload); case 6: _context.next = 8; return (0, _effects.put)({ __ns__: __ns__, type: _actions.types.CODE_EMAIL_SUCCESS }); case 8: _context.next = 15; break; case 10: _context.prev = 10; _context.t0 = _context['catch'](1); console.log(_context.t0.stack); _context.next = 15; return (0, _effects.put)({ __ns__: __ns__, type: _actions.types.CODE_EMAIL_FAILURE, payload: { error: _context.t0.message || _context.t0, stack: _context.t0.stack } }); case 15: case 'end': return _context.stop(); } } }, _marked[0], this, [[1, 10]]); } function watchCodeEmail() { var action; return regeneratorRuntime.wrap(function watchCodeEmail$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: if (!true) { _context2.next = 8; break; } _context2.next = 3; return (0, _effects.take)(_actions.types.CODE_EMAIL); case 3: action = _context2.sent; _context2.next = 6; return (0, _effects.fork)(codeEmail, action); case 6: _context2.next = 0; break; case 8: case 'end': return _context2.stop(); } } }, _marked[1], this); } function codeConfirm(action) { var __ns__; return regeneratorRuntime.wrap(function codeConfirm$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: __ns__ = action.__ns__; _context3.prev = 1; _context3.next = 4; return (0, _effects.put)({ __ns__: __ns__, type: _actions.types.CODE_CONFIRM_REQUEST }); case 4: _context3.next = 6; return (0, _effects.call)(api(__ns__).codeConfirm, action.payload); case 6: _context3.next = 8; return (0, _effects.put)({ __ns__: __ns__, type: _actions.types.CODE_CONFIRM_SUCCESS }); case 8: _context3.next = 15; break; case 10: _context3.prev = 10; _context3.t0 = _context3['catch'](1); console.log(_context3.t0.stack); _context3.next = 15; return (0, _effects.put)({ __ns__: __ns__, type: _actions.types.CODE_CONFIRM_FAILURE, payload: { error: _context3.t0.message || _context3.t0, stack: _context3.t0.stack } }); case 15: case 'end': return _context3.stop(); } } }, _marked[2], this, [[1, 10]]); } function watchCodeConfirm() { var action; return regeneratorRuntime.wrap(function watchCodeConfirm$(_context4) { while (1) { switch (_context4.prev = _context4.next) { case 0: if (!true) { _context4.next = 8; break; } _context4.next = 3; return (0, _effects.take)(_actions.types.CODE_CONFIRM); case 3: action = _context4.sent; _context4.next = 6; return (0, _effects.fork)(codeConfirm, action); case 6: _context4.next = 0; break; case 8: case 'end': return _context4.stop(); } } }, _marked[3], this); } function login(action) { var __ns__, username, _ref, sessionID; return regeneratorRuntime.wrap(function login$(_context5) { while (1) { switch (_context5.prev = _context5.next) { case 0: __ns__ = action.__ns__; _context5.prev = 1; username = action.payload.username; _context5.next = 5; return (0, _effects.put)({ __ns__: __ns__, type: _actions.types.LOGIN_REQUEST }); case 5: _context5.next = 7; return (0, _effects.call)(api(__ns__).login, action.payload); case 7: _ref = _context5.sent; sessionID = _ref.sessionID; _context5.next = 11; return (0, _effects.put)({ __ns__: __ns__, type: _actions.types.INSERT, payload: { sessionID: sessionID, username: username } }); case 11: _context5.next = 13; return (0, _effects.put)({ __ns__: __ns__, type: _actions.types.LOGIN_SUCCESS }); case 13: _context5.next = 20; break; case 15: _context5.prev = 15; _context5.t0 = _context5['catch'](1); console.log(_context5.t0.stack); _context5.next = 20; return (0, _effects.put)({ __ns__: __ns__, type: _actions.types.LOGIN_FAILURE, payload: { error: _context5.t0.message || _context5.t0, stack: _context5.t0.stack } }); case 20: case 'end': return _context5.stop(); } } }, _marked[4], this, [[1, 15]]); } function watchLogin() { var action; return regeneratorRuntime.wrap(function watchLogin$(_context6) { while (1) { switch (_context6.prev = _context6.next) { case 0: if (!true) { _context6.next = 8; break; } _context6.next = 3; return (0, _effects.take)(_actions.types.LOGIN); case 3: action = _context6.sent; _context6.next = 6; return (0, _effects.fork)(login, action); case 6: _context6.next = 0; break; case 8: case 'end': return _context6.stop(); } } }, _marked[5], this); } function logout(action) { var _this = this; var __ns__; return regeneratorRuntime.wrap(function logout$(_context8) { while (1) { switch (_context8.prev = _context8.next) { case 0: __ns__ = action.__ns__; _context8.prev = 1; return _context8.delegateYield(regeneratorRuntime.mark(function _callee() { var sessionID, state, auth, _auth$docs$find, _id; return regeneratorRuntime.wrap(function _callee$(_context7) { while (1) { switch (_context7.prev = _context7.next) { case 0: sessionID = action.payload.sessionID; _context7.next = 3; return (0, _effects.put)({ __ns__: __ns__, type: _actions.types.LOGOUT_REQUEST }); case 3: _context7.next = 5; return (0, _effects.call)(api(__ns__).logout, action.payload); case 5: _context7.next = 7; return (0, _effects.select)(function (state) { return state[__ns__]; }); case 7: state = _context7.sent; auth = (0, _select.select)(state); _auth$docs$find = auth.docs.find(function (doc) { return doc.sessionID == sessionID; }); _id = _auth$docs$find._id; if (_id) { _context7.next = 13; break; } throw new Error('Cannot find auth doc with sessionID = ' + sessionID); case 13: _context7.next = 15; return (0, _effects.put)({ __ns__: __ns__, type: _actions.types.REMOVE, payload: { _id: _id } }); case 15: _context7.next = 17; return (0, _effects.put)({ __ns__: __ns__, type: _actions.types.LOGOUT_SUCCESS }); case 17: case 'end': return _context7.stop(); } } }, _callee, _this); })(), 't0', 3); case 3: _context8.next = 10; break; case 5: _context8.prev = 5; _context8.t1 = _context8['catch'](1); console.log(_context8.t1.stack); _context8.next = 10; return (0, _effects.put)({ __ns__: __ns__, type: _actions.types.LOGOUT_FAILURE, payload: { error: _context8.t1.message || _context8.t1, stack: _context8.t1.stack } }); case 10: case 'end': return _context8.stop(); } } }, _marked[6], this, [[1, 5]]); } function watchLogout() { var action; return regeneratorRuntime.wrap(function watchLogout$(_context9) { while (1) { switch (_context9.prev = _context9.next) { case 0: if (!true) { _context9.next = 8; break; } _context9.next = 3; return (0, _effects.take)(_actions.types.LOGOUT); case 3: action = _context9.sent; _context9.next = 6; return (0, _effects.fork)(logout, action); case 6: _context9.next = 0; break; case 8: case 'end': return _context9.stop(); } } }, _marked[7], this); } function signup(action) { var __ns__; return regeneratorRuntime.wrap(function signup$(_context10) { while (1) { switch (_context10.prev = _context10.next) { case 0: __ns__ = action.__ns__; _context10.prev = 1; _context10.next = 4; return (0, _effects.put)({ __ns__: __ns__, type: _actions.types.SIGNUP_REQUEST }); case 4: _context10.next = 6; return (0, _effects.call)(api(__ns__).signup, action.payload); case 6: _context10.next = 8; return (0, _effects.put)({ __ns__: __ns__, type: _actions.types.SIGNUP_SUCCESS }); case 8: _context10.next = 15; break; case 10: _context10.prev = 10; _context10.t0 = _context10['catch'](1); console.log(_context10.t0.stack); _context10.next = 15; return (0, _effects.put)({ __ns__: __ns__, type: _actions.types.SIGNUP_FAILURE, payload: { error: _context10.t0.message || _context10.t0, stack: _context10.t0.stack } }); case 15: case 'end': return _context10.stop(); } } }, _marked[8], this, [[1, 10]]); } function watchSignup() { var action; return regeneratorRuntime.wrap(function watchSignup$(_context11) { while (1) { switch (_context11.prev = _context11.next) { case 0: if (!true) { _context11.next = 8; break; } _context11.next = 3; return (0, _effects.take)(_actions.types.SIGNUP); case 3: action = _context11.sent; _context11.next = 6; return (0, _effects.fork)(signup, action); case 6: _context11.next = 0; break; case 8: case 'end': return _context11.stop(); } } }, _marked[9], this); } function docsMaxOverRemove(action) { var __ns__, state, auth, docsMax, length, last; return regeneratorRuntime.wrap(function docsMaxOverRemove$(_context12) { while (1) { switch (_context12.prev = _context12.next) { case 0: __ns__ = action.__ns__; (0, _reduxJournal.write)('(action: { __ns__: \'' + __ns__ + '\' })', tags + '.docsMaxOverRemove'); _context12.next = 4; return (0, _effects.select)(function (state) { return state[__ns__]; }); case 4: state = _context12.sent; auth = (0, _select.select)(state); if (!auth.config.docsMaxOverRemove()) { _context12.next = 14; break; } docsMax = auth.config.docsMax(); length = auth.docs.length(); last = auth.docs.last(); if (!(length > docsMax)) { _context12.next = 14; break; } (0, _reduxJournal.write)('REMOVE ' + last._id, tags + '.docsMaxOverRemove'); _context12.next = 14; return (0, _effects.put)({ __ns__: __ns__, type: _actions.types.REMOVE, payload: { _id: last._id } }); case 14: case 'end': return _context12.stop(); } } }, _marked[10], this); } function watchInsert() { var action; return regeneratorRuntime.wrap(function watchInsert$(_context13) { while (1) { switch (_context13.prev = _context13.next) { case 0: if (!true) { _context13.next = 8; break; } _context13.next = 3; return (0, _effects.take)(_actions.types.INSERT); case 3: action = _context13.sent; _context13.next = 6; return (0, _effects.fork)(docsMaxOverRemove, action); case 6: _context13.next = 0; break; case 8: case 'end': return _context13.stop(); } } }, _marked[11], this); } function root() { return regeneratorRuntime.wrap(function root$(_context14) { while (1) { switch (_context14.prev = _context14.next) { case 0: _context14.next = 2; return (0, _effects.fork)(watchCodeConfirm); case 2: _context14.next = 4; return (0, _effects.fork)(watchCodeEmail); case 4: _context14.next = 6; return (0, _effects.fork)(watchLogin); case 6: _context14.next = 8; return (0, _effects.fork)(watchLogout); case 8: _context14.next = 10; return (0, _effects.fork)(watchSignup); case 10: _context14.next = 12; return (0, _effects.fork)(watchInsert); case 12: case 'end': return _context14.stop(); } } }, _marked[12], this); } return { root: root }; }; var saga = exports.saga = configSaga();