| author | ymh <ymh.work@gmail.com> |
| Tue, 29 Mar 2022 11:23:56 +0200 | |
| changeset 211 | 244a90638e80 |
| parent 168 | ea92f4fe783d |
| permissions | -rw-r--r-- |
|
87
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1 |
import * as types from '../constants/actionTypes'; |
|
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2 |
import { all, call, fork, race, take, cancelled, put, select } from 'redux-saga/effects' |
|
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
3 |
import config from '../config'; |
|
97
69eaef18b01b
Improve the network saga. Try to avoid unnecessary token refresh
ymh <ymh.work@gmail.com>
parents:
88
diff
changeset
|
4 |
import jwt_decode from 'jwt-decode'; |
|
69eaef18b01b
Improve the network saga. Try to avoid unnecessary token refresh
ymh <ymh.work@gmail.com>
parents:
88
diff
changeset
|
5 |
import moment from 'moment'; |
|
130
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
6 |
import { delay } from 'redux-saga'; |
|
129
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
97
diff
changeset
|
7 |
import { setOnlineStatus } from '../actions/networkActions'; |
|
134
be36eed5e6e0
add menu to change current group and create a new group
ymh <ymh.work@gmail.com>
parents:
130
diff
changeset
|
8 |
import { getToken } from '../selectors/authSelectors'; |
|
87
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
9 |
|
|
129
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
97
diff
changeset
|
10 |
|
|
87
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
11 |
|
|
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
12 |
function pingServer(client, token) { |
|
97
69eaef18b01b
Improve the network saga. Try to avoid unnecessary token refresh
ymh <ymh.work@gmail.com>
parents:
88
diff
changeset
|
13 |
const decodedToken = jwt_decode(token); |
|
69eaef18b01b
Improve the network saga. Try to avoid unnecessary token refresh
ymh <ymh.work@gmail.com>
parents:
88
diff
changeset
|
14 |
const currentTs = moment.now()/1000; |
|
69eaef18b01b
Improve the network saga. Try to avoid unnecessary token refresh
ymh <ymh.work@gmail.com>
parents:
88
diff
changeset
|
15 |
|
|
69eaef18b01b
Improve the network saga. Try to avoid unnecessary token refresh
ymh <ymh.work@gmail.com>
parents:
88
diff
changeset
|
16 |
const timeout = new Promise((resolve, reject) => { |
|
69eaef18b01b
Improve the network saga. Try to avoid unnecessary token refresh
ymh <ymh.work@gmail.com>
parents:
88
diff
changeset
|
17 |
setTimeout(reject, config.networkStatusTimeout, 'request timed out'); |
|
69eaef18b01b
Improve the network saga. Try to avoid unnecessary token refresh
ymh <ymh.work@gmail.com>
parents:
88
diff
changeset
|
18 |
}); |
|
69eaef18b01b
Improve the network saga. Try to avoid unnecessary token refresh
ymh <ymh.work@gmail.com>
parents:
88
diff
changeset
|
19 |
|
|
69eaef18b01b
Improve the network saga. Try to avoid unnecessary token refresh
ymh <ymh.work@gmail.com>
parents:
88
diff
changeset
|
20 |
if((decodedToken.exp-currentTs) < 300) { |
|
87
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
21 |
return Promise |
|
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
22 |
.race([timeout, client.post('/api/auth/refresh/', { token })]); |
|
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
23 |
} else { |
|
97
69eaef18b01b
Improve the network saga. Try to avoid unnecessary token refresh
ymh <ymh.work@gmail.com>
parents:
88
diff
changeset
|
24 |
// We do a GET because a HEAD generate a preflight CORS OPTION request. The GET does not. |
|
69eaef18b01b
Improve the network saga. Try to avoid unnecessary token refresh
ymh <ymh.work@gmail.com>
parents:
88
diff
changeset
|
25 |
return Promise |
|
69eaef18b01b
Improve the network saga. Try to avoid unnecessary token refresh
ymh <ymh.work@gmail.com>
parents:
88
diff
changeset
|
26 |
.race([timeout, client.get('/api/auth/user/')]); |
|
87
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
27 |
} |
|
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
28 |
} |
|
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
29 |
|
|
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
30 |
function* pollData(context) { |
|
129
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
97
diff
changeset
|
31 |
const token = yield select(getToken); |
|
97
69eaef18b01b
Improve the network saga. Try to avoid unnecessary token refresh
ymh <ymh.work@gmail.com>
parents:
88
diff
changeset
|
32 |
// No token : we wait for a login |
|
69eaef18b01b
Improve the network saga. Try to avoid unnecessary token refresh
ymh <ymh.work@gmail.com>
parents:
88
diff
changeset
|
33 |
if(!token) { |
|
69eaef18b01b
Improve the network saga. Try to avoid unnecessary token refresh
ymh <ymh.work@gmail.com>
parents:
88
diff
changeset
|
34 |
yield take(types.AUTH_LOGIN_SUCCESS); |
|
69eaef18b01b
Improve the network saga. Try to avoid unnecessary token refresh
ymh <ymh.work@gmail.com>
parents:
88
diff
changeset
|
35 |
} |
|
87
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
36 |
try { |
|
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
37 |
const res = yield pingServer(context.client, token); |
|
129
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
97
diff
changeset
|
38 |
yield put(setOnlineStatus(true)); |
|
97
69eaef18b01b
Improve the network saga. Try to avoid unnecessary token refresh
ymh <ymh.work@gmail.com>
parents:
88
diff
changeset
|
39 |
if(res.token) { |
|
69eaef18b01b
Improve the network saga. Try to avoid unnecessary token refresh
ymh <ymh.work@gmail.com>
parents:
88
diff
changeset
|
40 |
yield put({ |
|
69eaef18b01b
Improve the network saga. Try to avoid unnecessary token refresh
ymh <ymh.work@gmail.com>
parents:
88
diff
changeset
|
41 |
type: types.AUTH_STORE_TOKEN_ASYNC, |
|
69eaef18b01b
Improve the network saga. Try to avoid unnecessary token refresh
ymh <ymh.work@gmail.com>
parents:
88
diff
changeset
|
42 |
token: res.token, |
|
69eaef18b01b
Improve the network saga. Try to avoid unnecessary token refresh
ymh <ymh.work@gmail.com>
parents:
88
diff
changeset
|
43 |
}); |
|
69eaef18b01b
Improve the network saga. Try to avoid unnecessary token refresh
ymh <ymh.work@gmail.com>
parents:
88
diff
changeset
|
44 |
} |
|
87
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
45 |
} catch (error) { |
|
129
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
97
diff
changeset
|
46 |
yield put(setOnlineStatus(false)); |
|
97
69eaef18b01b
Improve the network saga. Try to avoid unnecessary token refresh
ymh <ymh.work@gmail.com>
parents:
88
diff
changeset
|
47 |
//TODO: This is ugly... |
|
137
279e1dffa213
session is now created with current group and protocol
ymh <ymh.work@gmail.com>
parents:
134
diff
changeset
|
48 |
const data = error.data; |
| 140 | 49 |
if ((data && data.non_field_errors && |
|
137
279e1dffa213
session is now created with current group and protocol
ymh <ymh.work@gmail.com>
parents:
134
diff
changeset
|
50 |
data.non_field_errors && |
|
279e1dffa213
session is now created with current group and protocol
ymh <ymh.work@gmail.com>
parents:
134
diff
changeset
|
51 |
data.non_field_errors.length && |
|
279e1dffa213
session is now created with current group and protocol
ymh <ymh.work@gmail.com>
parents:
134
diff
changeset
|
52 |
data.non_field_errors.length > 0 && |
|
279e1dffa213
session is now created with current group and protocol
ymh <ymh.work@gmail.com>
parents:
134
diff
changeset
|
53 |
( data.non_field_errors[0] === 'Signature has expired.' || |
|
279e1dffa213
session is now created with current group and protocol
ymh <ymh.work@gmail.com>
parents:
134
diff
changeset
|
54 |
data.non_field_errors[0] === 'Refresh has expired.' )) || |
|
97
69eaef18b01b
Improve the network saga. Try to avoid unnecessary token refresh
ymh <ymh.work@gmail.com>
parents:
88
diff
changeset
|
55 |
(error.detail && ( |
|
69eaef18b01b
Improve the network saga. Try to avoid unnecessary token refresh
ymh <ymh.work@gmail.com>
parents:
88
diff
changeset
|
56 |
error.detail === 'Signature has expired.' || |
|
69eaef18b01b
Improve the network saga. Try to avoid unnecessary token refresh
ymh <ymh.work@gmail.com>
parents:
88
diff
changeset
|
57 |
error.detail=== 'Refresh has expired.' |
|
69eaef18b01b
Improve the network saga. Try to avoid unnecessary token refresh
ymh <ymh.work@gmail.com>
parents:
88
diff
changeset
|
58 |
)) |
|
88
2a861fed6bde
Deauthenticate user if refresh was not possible (session expired or total delta reached)
ymh <ymh.work@gmail.com>
parents:
87
diff
changeset
|
59 |
) { |
|
2a861fed6bde
Deauthenticate user if refresh was not possible (session expired or total delta reached)
ymh <ymh.work@gmail.com>
parents:
87
diff
changeset
|
60 |
yield put({ |
|
2a861fed6bde
Deauthenticate user if refresh was not possible (session expired or total delta reached)
ymh <ymh.work@gmail.com>
parents:
87
diff
changeset
|
61 |
type: types.AUTH_DEAUTHENTICATE |
|
2a861fed6bde
Deauthenticate user if refresh was not possible (session expired or total delta reached)
ymh <ymh.work@gmail.com>
parents:
87
diff
changeset
|
62 |
}); |
|
87
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
63 |
} |
|
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
64 |
} finally { |
|
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
65 |
if (yield cancelled()) { |
|
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
66 |
// pollDate cancelled |
|
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
67 |
// if there is a token : this was a LOGIN, set status to ok |
|
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
68 |
// if there is no token : this was a LOGOUT, set status to ko and wait for login |
|
129
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
97
diff
changeset
|
69 |
const token = yield select(getToken); |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
97
diff
changeset
|
70 |
yield put(setOnlineStatus(Boolean(token))); |
|
97
69eaef18b01b
Improve the network saga. Try to avoid unnecessary token refresh
ymh <ymh.work@gmail.com>
parents:
88
diff
changeset
|
71 |
} |
|
69eaef18b01b
Improve the network saga. Try to avoid unnecessary token refresh
ymh <ymh.work@gmail.com>
parents:
88
diff
changeset
|
72 |
} |
|
69eaef18b01b
Improve the network saga. Try to avoid unnecessary token refresh
ymh <ymh.work@gmail.com>
parents:
88
diff
changeset
|
73 |
} |
|
69eaef18b01b
Improve the network saga. Try to avoid unnecessary token refresh
ymh <ymh.work@gmail.com>
parents:
88
diff
changeset
|
74 |
|
|
69eaef18b01b
Improve the network saga. Try to avoid unnecessary token refresh
ymh <ymh.work@gmail.com>
parents:
88
diff
changeset
|
75 |
function* callDelay(context) { |
|
69eaef18b01b
Improve the network saga. Try to avoid unnecessary token refresh
ymh <ymh.work@gmail.com>
parents:
88
diff
changeset
|
76 |
try { |
|
69eaef18b01b
Improve the network saga. Try to avoid unnecessary token refresh
ymh <ymh.work@gmail.com>
parents:
88
diff
changeset
|
77 |
yield call(delay, config.networkStatusInterval); |
|
69eaef18b01b
Improve the network saga. Try to avoid unnecessary token refresh
ymh <ymh.work@gmail.com>
parents:
88
diff
changeset
|
78 |
} finally { |
|
69eaef18b01b
Improve the network saga. Try to avoid unnecessary token refresh
ymh <ymh.work@gmail.com>
parents:
88
diff
changeset
|
79 |
if (yield cancelled()) { |
|
69eaef18b01b
Improve the network saga. Try to avoid unnecessary token refresh
ymh <ymh.work@gmail.com>
parents:
88
diff
changeset
|
80 |
// pollDate cancelled |
|
69eaef18b01b
Improve the network saga. Try to avoid unnecessary token refresh
ymh <ymh.work@gmail.com>
parents:
88
diff
changeset
|
81 |
// if there is a token : this was a LOGIN, set status to ok |
|
69eaef18b01b
Improve the network saga. Try to avoid unnecessary token refresh
ymh <ymh.work@gmail.com>
parents:
88
diff
changeset
|
82 |
// if there is no token : this was a LOGOUT, set status to ko and wait for login |
|
129
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
97
diff
changeset
|
83 |
const token = yield select(getToken); |
|
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
97
diff
changeset
|
84 |
yield put(setOnlineStatus(Boolean(token))); |
|
87
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
85 |
} |
|
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
86 |
} |
|
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
87 |
} |
|
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
88 |
|
|
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
89 |
// Wait for successful response, then fire another request |
|
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
90 |
// Cancel polling if user logs out or log in |
|
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
91 |
function* watchPollData(context) { |
|
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
92 |
while (true) { |
|
130
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
93 |
yield race({ |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
94 |
poll: all([call(pollData, context), call(callDelay, context)]), |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
95 |
logout: take(types.AUTH_LOGOUT), |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
96 |
login: take(types.AUTH_LOGIN_SUCCESS) |
|
78246db1cbac
make synchronization recurent, improve synchronization status display
ymh <ymh.work@gmail.com>
parents:
129
diff
changeset
|
97 |
}); |
|
87
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
98 |
} |
|
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
99 |
} |
|
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
100 |
|
|
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
101 |
// Daemonize tasks in parallel |
|
129
d48946d164c6
Add a first version of synchronisation
ymh <ymh.work@gmail.com>
parents:
97
diff
changeset
|
102 |
export default function* root(context) { |
|
87
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
103 |
yield all([ |
|
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
104 |
fork(watchPollData, context) |
|
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
105 |
// other watchers here |
|
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
106 |
]); |
|
dbcee57de2c6
Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
107 |
} |