diff options
| author | Pliable Pixels <pliablepixels@gmail.com> | 2019-07-08 11:36:37 -0400 |
|---|---|---|
| committer | Pliable Pixels <pliablepixels@gmail.com> | 2019-07-08 11:36:37 -0400 |
| commit | f683ea8023b71ab705ca37ea6ff60daa22c24a72 (patch) | |
| tree | 8a78a11dcb97fe0dbde3cc0d2d078a7194a3d368 | |
| parent | 6faf15bd652d0589e1fb30e559ad027525008e77 (diff) | |
#829 move to actual access token based timer
| -rw-r--r-- | www/js/NVR.js | 25 | ||||
| -rwxr-xr-x | www/js/app.js | 22 |
2 files changed, 40 insertions, 7 deletions
diff --git a/www/js/NVR.js b/www/js/NVR.js index e31fc52f..3a2b2dae 100644 --- a/www/js/NVR.js +++ b/www/js/NVR.js @@ -12,9 +12,10 @@ angular.module('zmApp.controllers') .service('NVR', ['$ionicPlatform', '$http', '$q', '$ionicLoading', '$ionicBackdrop', '$fileLogger', 'zm', '$rootScope', '$ionicContentBanner', '$timeout', '$cordovaPinDialog', '$ionicPopup', '$localstorage', '$state', '$translate', '$cordovaSQLite', function ($ionicPlatform, $http, $q, $ionicLoading, $ionicBackdrop, $fileLogger, zm, $rootScope, $ionicContentBanner, $timeout, $cordovaPinDialog, - $ionicPopup, $localstorage, $state, $translate) { + $ionicPopup, $localstorage, $state, $translate, $cordovaSQLite ) { var currentServerMultiPortSupported = false; + var tokenExpiryTimer = null; /* DO NOT TOUCH zmAppVersion @@ -475,6 +476,14 @@ angular.module('zmApp.controllers') loginData.accessTokenExpires = moment.utc().add(succ.access_token_expires, 'seconds'); loginData.refreshToken = succ.refresh_token; $rootScope.tokenExpires = succ.access_token_expires; + + log ('----> Setting token re-login after '+succ.access_token_expires+' seconds'); + if (tokenExpiryTimer) $timeout.cancel(tokenExpiryTimer); + //succ.access_token_expires = 30; + tokenExpiryTimer = $timeout ( function () { + $rootScope.$broadcast('token-expiry'); + }, succ.access_token_expires * 1000); + loginData.refreshTokenExpires = moment.utc().add(succ.refresh_token_expires, 'seconds'); @@ -2876,9 +2885,15 @@ angular.module('zmApp.controllers') // first see if we can work with access token if (moment.utc(loginData.accessTokenExpires).isAfter(now) && diff_access >=zm.accessTokenLeewayMin && tryAccess) { log ("Access token still has "+diff_access+" minutes left, using it"); + log ('----> Setting token re-login after '+diff_access*60+' seconds'); + if (tokenExpiryTimer) $timeout.cancel(tokenExpiryTimer); + tokenExpiryTimer = $timeout ( function () { + $rootScope.$broadcast('token-expiry'); + }, diff_access * 60 * 1000); + + // console.log ("**************** TOKEN SET="+loginData.accessToken); $rootScope.authSession = '&token='+loginData.accessToken; - $rootScope.tokenExpires = d.resolve("Login success via access token"); if (!noBroadcast) $rootScope.$broadcast('auth-success', '' ); return d.promise; @@ -2901,7 +2916,13 @@ angular.module('zmApp.controllers') log ("New access token retrieved: ..."+succ.access_token.substr(-5)); loginData.accessToken = succ.access_token; loginData.accessTokenExpires = moment.utc().add(succ.access_token_expires,'seconds'); + //succ.access_token_expires = 30; $rootScope.tokenExpires = succ.access_token_expires; + log ('----> Setting token re-login after '+succ.access_token_expires+' seconds'); + if (tokenExpiryTimer) $timeout.cancel(tokenExpiryTimer); + tokenExpiryTimer = $timeout ( function () { + $rootScope.$broadcast('token-expiry'); + }, succ.access_token_expires * 1000); log ("Current time is: UTC "+moment.utc().format("YYYY-MM-DD hh:mm:ss")); log ("New access token expires on: UTC "+loginData.accessTokenExpires.format("YYYY-MM-DD hh:mm:ss")); log ("New access token expires on:"+loginData.accessTokenExpires.format("YYYY-MM-DD hh:mm:ss")); diff --git a/www/js/app.js b/www/js/app.js index 550d3de9..206f5e16 100755 --- a/www/js/app.js +++ b/www/js/app.js @@ -114,8 +114,8 @@ angular.module('zmApp', [ androidBackupKey: "AEdPqrEAAAAIqF-OaHdwIzZhx2L1WOfAGTagBxm5a1R4wBW_Uw", accessTokenLeewayMin: 5, refreshTokenLeewayMin: 10, - defaultAccessTokenExpiresMs: 30000 - //defaultAccessTokenExpiresMs: 1800000 // half of 3600s + // defaultAccessTokenExpiresMs: 30000 + // defaultAccessTokenExpiresMs: 1800000 // half of 3600s }) @@ -884,6 +884,12 @@ angular.module('zmApp', [ // doLogin() emits this when there is an auth error in the portal //------------------------------------------------------------------ + $rootScope.$on("token-expiry", function () { + NVR.log ('-----> Access token is about to expire, re-doing login'); + _doLogin(""); + + }); + $rootScope.$on("auth-error", function () { NVR.debug("zmAutoLogin: Inside auth-error broadcast"); @@ -1105,12 +1111,18 @@ angular.module('zmApp', [ //$rootScope.loggedIntoZm = 0; var timeInterval = ld.isTokenSupported ? zm.defaultAccessTokenExpiresMs: zm.loginInterval; $interval.cancel(zmAutoLoginHandle); - //doLogin(); - NVR.debug ('We will relogin every '+timeInterval/1000+' seconds, token supported='+ld.isTokenSupported) + + if (ld.isTokenSupported) { + NVR.debug ("------> Not starting login timer for token. We will start a one time timer when we know how soon the access token will live"); + _doLogin(""); + + } else { + NVR.debug ('We will relogin every '+timeInterval/1000+' seconds, token supported='+ld.isTokenSupported) zmAutoLoginHandle = $interval(function () { _doLogin(""); }, timeInterval); - + + } } |
