| author | ymh <ymh.work@gmail.com> |
| Mon, 19 Jun 2017 21:37:33 +0200 | |
| changeset 58 | f16a080e0bc4 |
| parent 57 | 2e4e9f9ebc4f |
| child 59 | 1eb52770eefa |
| permissions | -rw-r--r-- |
| 29 | 1 |
import PouchDB from 'pouchdb' |
|
44
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
2 |
import { put, take, takeLatest, all } from 'redux-saga/effects' |
| 29 | 3 |
import * as types from '../constants/actionTypes'; |
4 |
import PouchDBFind from 'pouchdb-find'; |
|
5 |
import Immutable from 'immutable'; |
|
6 |
||
7 |
PouchDB.debug.disable(); |
|
8 |
PouchDB.plugin(PouchDBFind); |
|
9 |
||
10 |
const sessionsDB = new PouchDB('sessions'); |
|
11 |
const notesDB = new PouchDB('notes'); |
|
12 |
notesDB.createIndex({ |
|
13 |
index: { fields: ['session'] } |
|
14 |
}); |
|
15 |
||
16 |
// --- |
|
17 |
||
18 |
export function* loadSessions() { |
|
19 |
const response = yield sessionsDB.allDocs({ include_docs: true }) |
|
20 |
const sessions = response.rows.map(row => row.doc) |
|
21 |
yield put({ type: types.LOAD_SESSIONS, sessions: Immutable.List(sessions) }) |
|
22 |
} |
|
23 |
||
24 |
export function* watchLoadSessions() { |
|
25 |
yield takeLatest(types.LOAD_SESSIONS_ASYNC, loadSessions) |
|
26 |
} |
|
27 |
||
28 |
// --- |
|
29 |
||
30 |
export function* createSession(action) { |
|
|
58
f16a080e0bc4
on server, augment default token lifetime and add settings in .env to control it. Add the refresh endpoint
ymh <ymh.work@gmail.com>
parents:
57
diff
changeset
|
31 |
//const response = yield sessionsDB.put(action.session); |
| 29 | 32 |
// TODO Error control |
|
58
f16a080e0bc4
on server, augment default token lifetime and add settings in .env to control it. Add the refresh endpoint
ymh <ymh.work@gmail.com>
parents:
57
diff
changeset
|
33 |
//const session = Object.assign({}, action.session, { rev: response.rev }); |
|
f16a080e0bc4
on server, augment default token lifetime and add settings in .env to control it. Add the refresh endpoint
ymh <ymh.work@gmail.com>
parents:
57
diff
changeset
|
34 |
//yield put({ type: types.CREATE_SESSION, session: session }) |
|
f16a080e0bc4
on server, augment default token lifetime and add settings in .env to control it. Add the refresh endpoint
ymh <ymh.work@gmail.com>
parents:
57
diff
changeset
|
35 |
yield console.log("CREATE SESSION", action); |
| 29 | 36 |
} |
37 |
||
38 |
export function* watchCreateSession() { |
|
39 |
yield takeLatest(types.CREATE_SESSION_ASYNC, createSession) |
|
40 |
} |
|
41 |
||
42 |
// --- |
|
43 |
||
|
30
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
44 |
export function* updateSession(action) { |
|
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
45 |
|
|
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
46 |
const { _id } = action.session; |
|
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
47 |
let session; |
|
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
48 |
|
| 40 | 49 |
const doc = yield sessionsDB.get(_id); |
50 |
session = Object.assign({}, doc, action.values); |
|
51 |
const response = yield sessionsDB.put(session); |
|
|
30
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
52 |
|
|
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
53 |
yield put({ |
|
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
54 |
type: types.UPDATE_SESSION, |
|
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
55 |
session: Object.assign({}, session, { rev: response.rev }) |
|
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
56 |
}) |
|
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
57 |
} |
|
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
58 |
|
|
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
59 |
export function* watchUpdateSession() { |
|
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
60 |
yield takeLatest(types.UPDATE_SESSION_ASYNC, updateSession) |
|
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
61 |
} |
|
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
62 |
|
|
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
63 |
// --- |
|
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
64 |
|
| 29 | 65 |
export function* addNote(action) { |
66 |
const response = yield notesDB.put(action.note); |
|
67 |
// TODO Error control |
|
68 |
const note = Object.assign({}, action.note, { rev: response.rev }); |
|
69 |
yield put({ type: types.ADD_NOTE, note: note }) |
|
70 |
} |
|
71 |
||
72 |
export function* watchAddNote() { |
|
73 |
yield takeLatest(types.ADD_NOTE_ASYNC, addNote) |
|
74 |
} |
|
75 |
||
76 |
// --- |
|
77 |
||
78 |
export function* loadNotesBySession(action) { |
|
79 |
const result = yield notesDB.find({ |
|
80 |
selector: { session: action.session._id }, |
|
81 |
// sort: ['name'] |
|
82 |
}); |
|
83 |
yield put({ type: types.LOAD_NOTES_BY_SESSION, notes: Immutable.List(result.docs) }) |
|
84 |
} |
|
85 |
||
86 |
export function* watchLoadNotesBySession() { |
|
87 |
yield takeLatest(types.LOAD_NOTES_BY_SESSION_ASYNC, loadNotesBySession) |
|
88 |
} |
|
89 |
||
90 |
// --- |
|
91 |
||
|
44
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
92 |
export function* watchLoginSubmit() { |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
93 |
while (true) { |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
94 |
const { username, password } = yield take(types.AUTH_LOGIN_SUBMIT); |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
95 |
yield put({ type: types.AUTH_LOGIN_REQUEST, username, password }); |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
96 |
} |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
97 |
} |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
98 |
|
| 55 | 99 |
function* watchLoginRequest(context) { |
|
44
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
100 |
while (true) { |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
101 |
try { |
| 47 | 102 |
|
|
44
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
103 |
const { username, password } = yield take(types.AUTH_LOGIN_REQUEST); |
| 57 | 104 |
const client = context.client; |
|
44
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
105 |
const response = yield client.post('/api/auth/login/', { username, password }); |
| 47 | 106 |
|
107 |
localStorage.setItem('currentUser', JSON.stringify(response.user)); |
|
108 |
localStorage.setItem('token', response.token); |
|
109 |
||
110 |
const actions = [{ |
|
|
44
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
111 |
type: types.AUTH_LOGIN_SUCCESS, |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
112 |
user: response.user, |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
113 |
token: response.token, |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
114 |
meta: { |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
115 |
transition: (prevState, nextState, action) => ({ |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
116 |
pathname: '/sessions', |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
117 |
}), |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
118 |
}, |
| 47 | 119 |
}, { |
120 |
type: types.AUTH_STORE_TOKEN_ASYNC, |
|
121 |
token: response.token, |
|
122 |
}]; |
|
123 |
||
|
56
96543c395baa
Use token in storage state, not in localStorage
ymh <ymh.work@gmail.com>
parents:
55
diff
changeset
|
124 |
yield all(actions.map(action => put(action))) |
| 47 | 125 |
|
|
44
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
126 |
} catch(e) { |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
127 |
yield put({ type: types.AUTH_LOGIN_ERROR, error: e }); |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
128 |
} |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
129 |
} |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
130 |
} |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
131 |
|
| 47 | 132 |
function* watchStoreToken() { |
133 |
while (true) { |
|
134 |
const { token } = yield take(types.AUTH_STORE_TOKEN_ASYNC); |
|
135 |
yield put({ type: types.AUTH_STORE_TOKEN, token }); |
|
136 |
} |
|
137 |
} |
|
138 |
||
| 57 | 139 |
function* watchUpdateSettings(context) { |
| 53 | 140 |
while (true) { |
141 |
const { username, firstname, lastname } = yield take(types.USER_UPDATE_SETTINGS_ASYNC); |
|
| 57 | 142 |
const client = context.client; |
| 53 | 143 |
try { |
144 |
yield client.put('/api/auth/user/', { |
|
145 |
username, |
|
146 |
first_name: firstname, |
|
147 |
last_name: lastname |
|
148 |
}); |
|
149 |
yield put({ type: types.USER_UPDATE_SETTINGS, firstname, lastname }); |
|
150 |
} catch (e) { |
|
151 |
||
152 |
} |
|
153 |
} |
|
154 |
} |
|
155 |
||
|
44
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
156 |
// --- |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
157 |
|
| 55 | 158 |
export default function* rootSaga(context) { |
| 29 | 159 |
yield all([ |
160 |
watchLoadSessions(), |
|
161 |
watchLoadNotesBySession(), |
|
162 |
watchAddNote(), |
|
163 |
watchCreateSession(), |
|
|
30
4d93f4ed95bc
Update session in PouchDB.
Alexandre Segura <mex.zktk@gmail.com>
parents:
29
diff
changeset
|
164 |
watchUpdateSession(), |
|
44
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
165 |
watchLoginSubmit(), |
| 55 | 166 |
watchLoginRequest(context), |
| 47 | 167 |
watchStoreToken(), |
| 57 | 168 |
watchUpdateSettings(context), |
| 29 | 169 |
]) |
170 |
} |