mongodb-stitch
Version:
[](https://gitter.im/mongodb/stitch?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
52 lines • 1.64 kB
JavaScript
import JWT from "./JWT";
var SLEEP_MILLIS = 60000;
var EXPIRATION_WINDOW_SECS = 300;
var AccessTokenRefresher = (function () {
function AccessTokenRefresher(auth) {
this.auth = auth;
}
AccessTokenRefresher.prototype.shouldRefresh = function () {
var auth = this.auth;
if (auth === undefined) {
return false;
}
if (!auth.isLoggedIn) {
return false;
}
var info = auth.authInfo;
if (info === undefined) {
return false;
}
var jwt;
try {
jwt = JWT.fromEncoded(info.accessToken);
}
catch (e) {
console.log(e);
return false;
}
if (Date.now() / 1000 < jwt.expires - EXPIRATION_WINDOW_SECS) {
return false;
}
return true;
};
AccessTokenRefresher.prototype.run = function () {
var _this = this;
if (!this.shouldRefresh()) {
this.nextTimeout = setTimeout(function () { return _this.run(); }, SLEEP_MILLIS);
}
else {
this.auth.refreshAccessToken().then(function () {
_this.nextTimeout = setTimeout(function () { return _this.run(); }, SLEEP_MILLIS);
}).catch(function () {
_this.nextTimeout = setTimeout(function () { return _this.run(); }, SLEEP_MILLIS);
});
}
};
AccessTokenRefresher.prototype.stop = function () {
clearTimeout(this.nextTimeout);
};
return AccessTokenRefresher;
}());
export default AccessTokenRefresher;
//# sourceMappingURL=AccessTokenRefresher.js.map