Skip to content

Commit

Permalink
Fix when the auth is not working
Browse files Browse the repository at this point in the history
  • Loading branch information
Frigyes Bartha committed Jun 30, 2020
1 parent 0d643bf commit a3e5bf3
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions carelink.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,17 @@ var Client = exports.Client = function (options) {
Authorization: "Bearer " + _.get(getCookie(CARELINKEU_TOKEN_COOKIE), 'value', ''),
},
}),
checkResponseThen(next)
function (err, response) {
err = err || responseAsError(response);

if (err) {
// reset cookie jar and do the login again
jar = request.jar();
checkLogin(next);
} else {
next();
}
},
);
}

Expand Down Expand Up @@ -282,7 +292,13 @@ var Client = exports.Client = function (options) {
var timeout = retryDurationOnAttempt(retryCount);
logger.log('Trying again in ' + timeout + ' second(s)...');
setTimeout(function () {
getConnectData(response, next, retryCount + 1);
if (CARELINK_EU) {
refreshTokenEu(function() {
getConnectData(response, next, retryCount + 1);
});
} else {
getConnectData(response, next, retryCount + 1);
}
}, 1000 * timeout);
} else {
next(null, response);
Expand All @@ -305,9 +321,9 @@ var Client = exports.Client = function (options) {
if (CARELINK_EU) {
// EU - SSO method
if (haveCookie(CARELINKEU_TOKEN_COOKIE)) {
let expire = new Date(Date.parse( _.get(getCookie(CARELINKEU_TOKENEXPIRE_COOKIE), 'value', '1970-01-01')));
let expire = new Date(Date.parse(_.get(getCookie(CARELINKEU_TOKENEXPIRE_COOKIE), 'value', '2999-01-01')));

if (expire < new Date(Date.now() - 5 * 1000 * 60)) {
if (expire < new Date(Date.now() - 10 * 1000 * 60)) {
refreshTokenEu(next);
} else {
next(null, null);
Expand Down

0 comments on commit a3e5bf3

Please sign in to comment.