--- a/client/src/sagas/index.js Thu Jun 15 17:18:22 2017 +0200
+++ b/client/src/sagas/index.js Fri Jun 16 18:36:39 2017 +0200
@@ -1,8 +1,9 @@
import PouchDB from 'pouchdb'
-import { put, takeLatest, all } from 'redux-saga/effects'
+import { put, take, takeLatest, all } from 'redux-saga/effects'
import * as types from '../constants/actionTypes';
import PouchDBFind from 'pouchdb-find';
import Immutable from 'immutable';
+import APIClient from '../APIClient';
PouchDB.debug.disable();
PouchDB.plugin(PouchDBFind);
@@ -13,6 +14,8 @@
index: { fields: ['session'] }
});
+const client = new APIClient('http://localhost:8000')
+
// ---
export function* loadSessions() {
@@ -88,6 +91,36 @@
// ---
+export function* watchLoginSubmit() {
+ while (true) {
+ const { username, password } = yield take(types.AUTH_LOGIN_SUBMIT);
+ yield put({ type: types.AUTH_LOGIN_REQUEST, username, password });
+ }
+}
+
+function* watchLoginRequest() {
+ while (true) {
+ try {
+ const { username, password } = yield take(types.AUTH_LOGIN_REQUEST);
+ const response = yield client.post('/api/auth/login/', { username, password });
+ yield put({
+ type: types.AUTH_LOGIN_SUCCESS,
+ user: response.user,
+ token: response.token,
+ meta: {
+ transition: (prevState, nextState, action) => ({
+ pathname: '/sessions',
+ }),
+ },
+ });
+ } catch(e) {
+ yield put({ type: types.AUTH_LOGIN_ERROR, error: e });
+ }
+ }
+}
+
+// ---
+
export default function* rootSaga() {
yield all([
watchLoadSessions(),
@@ -95,5 +128,7 @@
watchAddNote(),
watchCreateSession(),
watchUpdateSession(),
+ watchLoginSubmit(),
+ watchLoginRequest(),
])
}