| author | ymh <ymh.work@gmail.com> |
| Tue, 20 Jun 2017 14:13:15 +0200 | |
| changeset 62 | b2514a9bcd49 |
| parent 59 | 1eb52770eefa |
| permissions | -rw-r--r-- |
|
62
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
1 |
import { put, take, all } from 'redux-saga/effects' |
| 29 | 2 |
import * as types from '../constants/actionTypes'; |
3 |
||
4 |
// --- |
|
5 |
||
|
44
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
6 |
export function* watchLoginSubmit() { |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
7 |
while (true) { |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
8 |
const { username, password } = yield take(types.AUTH_LOGIN_SUBMIT); |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
9 |
yield put({ type: types.AUTH_LOGIN_REQUEST, username, password }); |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
10 |
} |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
11 |
} |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
12 |
|
| 55 | 13 |
function* watchLoginRequest(context) { |
|
44
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
14 |
while (true) { |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
15 |
try { |
| 47 | 16 |
|
|
44
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
17 |
const { username, password } = yield take(types.AUTH_LOGIN_REQUEST); |
| 57 | 18 |
const client = context.client; |
|
44
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
19 |
const response = yield client.post('/api/auth/login/', { username, password }); |
| 47 | 20 |
|
21 |
const actions = [{ |
|
|
44
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
22 |
type: types.AUTH_LOGIN_SUCCESS, |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
23 |
user: response.user, |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
24 |
token: response.token, |
|
62
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
25 |
// meta: { |
|
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
26 |
// transition: (prevState, nextState, action) => ({ |
|
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
27 |
// pathname: '/sessions', |
|
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
28 |
// }), |
|
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
29 |
// }, |
| 47 | 30 |
}, { |
31 |
type: types.AUTH_STORE_TOKEN_ASYNC, |
|
32 |
token: response.token, |
|
33 |
}]; |
|
34 |
||
|
62
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
35 |
yield all(actions.map(action => put(action))); |
|
b2514a9bcd49
migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents:
59
diff
changeset
|
36 |
context.history.push('/sessions'); |
| 47 | 37 |
|
|
44
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
38 |
} catch(e) { |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
39 |
yield put({ type: types.AUTH_LOGIN_ERROR, error: e }); |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
40 |
} |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
41 |
} |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
42 |
} |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
43 |
|
| 47 | 44 |
function* watchStoreToken() { |
45 |
while (true) { |
|
46 |
const { token } = yield take(types.AUTH_STORE_TOKEN_ASYNC); |
|
47 |
yield put({ type: types.AUTH_STORE_TOKEN, token }); |
|
48 |
} |
|
49 |
} |
|
50 |
||
| 57 | 51 |
function* watchUpdateSettings(context) { |
| 53 | 52 |
while (true) { |
53 |
const { username, firstname, lastname } = yield take(types.USER_UPDATE_SETTINGS_ASYNC); |
|
| 57 | 54 |
const client = context.client; |
| 53 | 55 |
try { |
56 |
yield client.put('/api/auth/user/', { |
|
57 |
username, |
|
58 |
first_name: firstname, |
|
59 |
last_name: lastname |
|
60 |
}); |
|
61 |
yield put({ type: types.USER_UPDATE_SETTINGS, firstname, lastname }); |
|
62 |
} catch (e) { |
|
63 |
||
64 |
} |
|
65 |
} |
|
66 |
} |
|
67 |
||
|
44
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
68 |
// --- |
|
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
69 |
|
| 55 | 70 |
export default function* rootSaga(context) { |
| 29 | 71 |
yield all([ |
|
44
3b20e2b584fe
Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents:
40
diff
changeset
|
72 |
watchLoginSubmit(), |
| 55 | 73 |
watchLoginRequest(context), |
| 47 | 74 |
watchStoreToken(), |
| 57 | 75 |
watchUpdateSettings(context), |
| 29 | 76 |
]) |
77 |
} |