UNPKG

redux-security

Version:
161 lines (143 loc) 4.95 kB
import { write, error } from 'redux-journal' import { manager } from 'redux-manager' import { call, fork, put, take } from 'redux-saga/effects' import { select as selectState } from 'redux-saga/effects' import { types } from './actions' import { TAGS } from './config' import { select } from './select' const tags = `${TAGS}.saga` export const configSaga = () => { const api = (serviceName) => { const API = manager.api.get(serviceName) if (!API) { const ERROR = `manager.api.get('${serviceName}') == undefined` error(ERROR, `${tags}.configSaga.api`) throw new Error(ERROR) } return API } function *codeEmail(action) { const { __ns__ } = action try { yield put({ __ns__, type: types.CODE_EMAIL_REQUEST }) yield call(api(__ns__).codeEmail, action.payload) yield put({ __ns__, type: types.CODE_EMAIL_SUCCESS }) } catch (e) { console.log(e.stack) yield put({ __ns__, type: types.CODE_EMAIL_FAILURE, payload: { error: e.message || e, stack: e.stack }}) } } function *watchCodeEmail() { while (true) { const action = yield take(types.CODE_EMAIL) yield fork(codeEmail, action) } } function *codeConfirm(action) { const { __ns__ } = action try { yield put({ __ns__, type: types.CODE_CONFIRM_REQUEST }) yield call(api(__ns__).codeConfirm, action.payload) yield put({ __ns__, type: types.CODE_CONFIRM_SUCCESS }) } catch (e) { console.log(e.stack) yield put({ __ns__, type: types.CODE_CONFIRM_FAILURE, payload: { error: e.message || e, stack: e.stack }}) } } function *watchCodeConfirm() { while (true) { const action = yield take(types.CODE_CONFIRM) yield fork(codeConfirm, action) } } function *login(action) { const { __ns__ } = action try { const { payload: { username }} = action yield put({ __ns__, type: types.LOGIN_REQUEST }) const { sessionID } = yield call(api(__ns__).login, action.payload) yield put({ __ns__, type: types.INSERT, payload: { sessionID, username }}) yield put({ __ns__, type: types.LOGIN_SUCCESS }) } catch (e) { console.log(e.stack) yield put({ __ns__, type: types.LOGIN_FAILURE, payload: { error: e.message || e, stack: e.stack }}) } } function *watchLogin() { while (true) { const action = yield take(types.LOGIN) yield fork(login, action) } } function *logout(action) { const { __ns__ } = action try { const { payload: { sessionID }} = action yield put({ __ns__, type: types.LOGOUT_REQUEST }) yield call(api(__ns__).logout, action.payload) const state = yield selectState(state => state[__ns__]) const auth = select(state) const { _id } = auth.docs.find((doc) => doc.sessionID == sessionID ) if (!_id) throw new Error(`Cannot find auth doc with sessionID = ${sessionID}`) yield put({ __ns__, type: types.REMOVE, payload: { _id }}) yield put({ __ns__, type: types.LOGOUT_SUCCESS }) } catch (e) { console.log(e.stack) yield put({ __ns__, type: types.LOGOUT_FAILURE, payload: { error: e.message || e, stack: e.stack }}) } } function *watchLogout() { while (true) { const action = yield take(types.LOGOUT) yield fork(logout, action) } } function *signup(action) { const { __ns__ } = action try { yield put({ __ns__, type: types.SIGNUP_REQUEST }) yield call(api(__ns__).signup, action.payload) yield put({ __ns__, type: types.SIGNUP_SUCCESS }) } catch (e) { console.log(e.stack) yield put({ __ns__, type: types.SIGNUP_FAILURE, payload: { error: e.message || e, stack: e.stack }}) } } function *watchSignup() { while (true) { const action = yield take(types.SIGNUP) yield fork(signup, action) } } function *docsMaxOverRemove(action) { const { __ns__ } = action write(`(action: { __ns__: '${__ns__}' })`, `${tags}.docsMaxOverRemove`) const state = yield selectState(state => state[__ns__]) const auth = select(state) if (auth.config.docsMaxOverRemove()) { const docsMax = auth.config.docsMax() const length = auth.docs.length() const last = auth.docs.last() if (length > docsMax) { write(`REMOVE ${last._id}`, `${tags}.docsMaxOverRemove`) yield put({ __ns__, type: types.REMOVE, payload: { _id: last._id }}) } } } function *watchInsert() { while (true) { const action = yield take(types.INSERT) yield fork(docsMaxOverRemove, action) } } function *root() { yield fork(watchCodeConfirm) yield fork(watchCodeEmail) yield fork(watchLogin) yield fork(watchLogout) yield fork(watchSignup) yield fork(watchInsert) } return { root } } export const saga = configSaga()