Skip to content

Commit

Permalink
KAZUI-133: handle machine sleep (delays timers) - check every 60s for…
Browse files Browse the repository at this point in the history
… refresh threshold (#67)
  • Loading branch information
danielfinke authored and JRMaitre committed Jan 29, 2018
1 parent 04b9dea commit 96ec6b1
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions js/winkstart-util.js
Expand Up @@ -711,25 +711,32 @@
*/
winkstart.autoRefreshAuth = function() {
/**
* Schedule an auth token refresh slightly before the current token's
* expiry.
* Check time between token expiry and current time. If < threshold,
* perform a refresh. Schedule another check for 60s later.
*/
function scheduleRefresh() {
function checkRefresh() {
/**
* If auth expiry is in less than $threshold seconds, refresh auth.
*/
var threshold = 600;

var now = (new Date()).getTime() / 1000,
authData = jwt_decode(winkstart.apps['auth'].auth_token),
// Refresh auth token slightly before its expiry
refreshIn = Math.floor((authData.exp - now) * 0.9);

if(refreshIn > 0) {
winkstart.log('Next auth refresh in ' + refreshIn + ' seconds');
diff = Math.floor(authData.exp - now);

setTimeout(refreshAuth, refreshIn * 1000);
if(diff < threshold) {
refreshAuth();
}
else {
console.log('Next auth refresh in ~' + (diff - threshold) + ' seconds');
setTimeout(checkRefresh, 60000);
}
}

/**
* Perform a refresh_token auth action. Update auth_token in apps and
* schedule the next refresh.
* schedule the next check.
*/
function refreshAuth() {
winkstart.request('auth.action', {
Expand All @@ -747,15 +754,15 @@

$.cookie('c_winkstart_auth', JSON.stringify(winkstart.apps['auth']));

scheduleRefresh();
setTimeout(checkRefresh, 60000);
},
function(data, status) {
scheduleRefresh();
setTimeout(checkRefresh, 60000);
}
);
}

scheduleRefresh();
checkRefresh();
};

})(window.winkstart = window.winkstart || {}, window.amplify = window.amplify || {}, jQuery);

0 comments on commit 96ec6b1

Please sign in to comment.