--- a/client/src/sagas/networkSaga.js Tue Jun 27 10:54:04 2017 +0200
+++ b/client/src/sagas/networkSaga.js Tue Jun 27 11:38:26 2017 +0200
@@ -1,6 +1,9 @@
import * as types from '../constants/actionTypes';
import { all, call, fork, race, take, cancelled, put, select } from 'redux-saga/effects'
import config from '../config';
+import * as persistConstants from 'redux-persist/constants';
+import jwt_decode from 'jwt-decode';
+import moment from 'moment';
// Utility function to delay effects
function delay(millis) {
@@ -11,39 +14,51 @@
}
function pingServer(client, token) {
- if(token) {
- const timeout = new Promise((resolve, reject) => {
- setTimeout(reject, config.networkStatusTimeout, 'request timed out');
- });
+ const decodedToken = jwt_decode(token);
+ const currentTs = moment.now()/1000;
+
+ const timeout = new Promise((resolve, reject) => {
+ setTimeout(reject, config.networkStatusTimeout, 'request timed out');
+ });
+
+ if((decodedToken.exp-currentTs) < 300) {
return Promise
.race([timeout, client.post('/api/auth/refresh/', { token })]);
} else {
- return Promise.reject({ error: 'No token in the store'})
+ // We do a GET because a HEAD generate a preflight CORS OPTION request. The GET does not.
+ return Promise
+ .race([timeout, client.get('/api/auth/user/')]);
}
}
-// Fetch data every 20 seconds
function* pollData(context) {
+ const token = yield select(state => state.token);
+ // No token : we wait for a login
+ if(!token) {
+ yield take(types.AUTH_LOGIN_SUCCESS);
+ }
try {
- yield call(delay, config.networkStatusInterval);
- const token = yield select(state => state.token);
const res = yield pingServer(context.client, token);
yield call(context.callback, true);
- yield put({
- type: types.AUTH_STORE_TOKEN_ASYNC,
- token: res.token,
- });
+ if(res.token) {
+ yield put({
+ type: types.AUTH_STORE_TOKEN_ASYNC,
+ token: res.token,
+ });
+ }
} catch (error) {
yield call(context.callback, false);
- // if the error is that there is no token, then we know we have to wait for a login
- if(error.error && error.error === 'No token in the store') {
- yield take(types.AUTH_LOGIN_SUCCESS);
- } else if (error.non_field_errors &&
+ //TODO: This is ugly...
+ if ((error.non_field_errors &&
error.non_field_errors &&
error.non_field_errors.length &&
error.non_field_errors.length > 0 &&
( error.non_field_errors[0] === 'Signature has expired.' ||
- error.non_field_errors[0] === 'Refresh has expired.' )
+ error.non_field_errors[0] === 'Refresh has expired.' )) ||
+ (error.detail && (
+ error.detail === 'Signature has expired.' ||
+ error.detail=== 'Refresh has expired.'
+ ))
) {
yield put({
type: types.AUTH_DEAUTHENTICATE
@@ -55,12 +70,21 @@
// if there is a token : this was a LOGIN, set status to ok
// if there is no token : this was a LOGOUT, set status to ko and wait for login
const token = yield select(state => state.token);
- if(token) {
- yield call(context.callback, true);
- } else {
- yield call(context.callback, false);
- yield take(types.AUTH_LOGIN_SUCCESS);
- }
+ yield call(context.callback, Boolean(token));
+ }
+ }
+}
+
+function* callDelay(context) {
+ try {
+ yield call(delay, config.networkStatusInterval);
+ } finally {
+ if (yield cancelled()) {
+ // pollDate cancelled
+ // if there is a token : this was a LOGIN, set status to ok
+ // if there is no token : this was a LOGOUT, set status to ko and wait for login
+ const token = yield select(state => state.token);
+ yield call(context.callback, Boolean(token));
}
}
}
@@ -68,9 +92,13 @@
// Wait for successful response, then fire another request
// Cancel polling if user logs out or log in
function* watchPollData(context) {
+
+ //wait for the state to be rehydrated
+ yield take(persistConstants.REHYDRATE);
+
while (true) {
yield race([
- call(pollData, context),
+ all([call(pollData, context), call(callDelay, context)]),
take(types.AUTH_LOGOUT),
take(types.AUTH_LOGIN_SUCCESS)
]);