Skip to content

Commit

Permalink
POC - try authenticating with prefetched token
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnbrouwers committed Oct 4, 2023
1 parent 73e1b01 commit 38d02a8
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions carelink.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var DEFAULT_CARELINKSERVERADDRESS = MMCONNECT_SERVERNAME || (CARELINK_EU ? "care

var DEFAULT_COUNTRYCODE = process.env['MMCONNECT_COUNTRYCODE'] || 'gb';
var DEFAULT_LANGCODE = process.env['MMCONNECT_LANGCODE'] || 'en';

var FIRST_TIME_LOGIN = true;

var Client = exports.Client = function (options) {
var CARELINKEU_LOGIN_LOCALE = { country: options.countrycode || DEFAULT_COUNTRYCODE
Expand Down Expand Up @@ -109,10 +109,16 @@ var Client = exports.Client = function (options) {
}

function haveCookie(cookieName) {
if(FIRST_TIME_LOGIN && process.env[cookieName]) {
return true;
}
return _.some(getCookies(), {key: cookieName});
}

function getCookie(cookieName) {
if(FIRST_TIME_LOGIN && process.env[cookieName]) {
return {value:process.env[cookieName]};
}
return _.find(getCookies(), {key: cookieName});
}

Expand Down Expand Up @@ -222,8 +228,18 @@ var Client = exports.Client = function (options) {
logger.log('Refresh EU token');

return await axiosInstance
.post(CARELINKEU_REFRESH_TOKEN_URL)
.post(CARELINKEU_REFRESH_TOKEN_URL,null,{
headers: {
'Authorization': `Bearer ${_.get(getCookie(CARELINKEU_TOKEN_COOKIE), 'value', '')}`,
'Content-Type': `application/json; charset=utf-8`
},
params: {
locale: DEFAULT_LANGCODE,
country: DEFAULT_COUNTRYCODE
}
})
.then(response => {
FIRST_TIME_LOGIN = false;
axiosInstance.defaults.headers.common = {
'Authorization': `Bearer ${_.get(getCookie(CARELINKEU_TOKEN_COOKIE), 'value', '')}`,
'Cookie': ''
Expand Down Expand Up @@ -275,7 +291,7 @@ var Client = exports.Client = function (options) {
let expire = new Date(Date.parse(_.get(getCookie(CARELINKEU_TOKENEXPIRE_COOKIE), 'value')));

// Refresh token if expires in 6 minutes
if (expire < new Date(Date.now() + 6 * 60 * 1000))
if (expire < new Date(Date.now() + 6 * 60 * 1000) || FIRST_TIME_LOGIN)
await refreshTokenEu();
} else {
logger.log('Logging in to CareLink');
Expand All @@ -293,6 +309,7 @@ var Client = exports.Client = function (options) {
await doFetchCookie(response)
}
}
FIRST_TIME_LOGIN = false;
}

function sleep(ms) {
Expand Down

0 comments on commit 38d02a8

Please sign in to comment.