Skip to content

Commit 65ced91

Browse files
author
Sachin Maheshwari
committed
matching cookie expiry time with token expiry and introducing 'mode' to distinguish between signIn and signUp
1 parent 1cf4b67 commit 65ced91

File tree

2 files changed

+62
-9
lines changed

2 files changed

+62
-9
lines changed

web-assets/js/.DS_Store

6 KB
Binary file not shown.

web-assets/js/setupAuth0WithRedirect.js

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@ const authSetup = function () {
3838
const utmSource = qs['utm_source'];
3939
const utmMedium = qs['utm_medium'];
4040
const utmCampaign = qs['utm_campaign'];
41-
const appUrl = qs['appUrl'] || false;
4241
const loggerMode = "dev";
4342
const IframeLogoutRequestType = "LOGOUT_REQUEST";
4443
const enterpriseCustomers = ['zurich', 'cs'];
44+
const mode = qs['mode'] || 'signIn';
4545
let returnAppUrl = qs['retUrl'];
46+
let appUrl = qs['appUrl'] || false;
4647

4748
if (utmSource &&
4849
(utmSource != 'undefined') &&
@@ -185,7 +186,8 @@ const authSetup = function () {
185186
utmSource: utmSource,
186187
utmCampaign: utmCampaign,
187188
utmMedium: utmMedium,
188-
returnUrl: returnAppUrl
189+
returnUrl: returnAppUrl,
190+
mode: mode
189191
})
190192
.then(function () {
191193
auth0.isAuthenticated().then(function (isAuthenticated) {
@@ -272,9 +274,25 @@ const authSetup = function () {
272274
return false;
273275
});
274276
logger('Storing token...', true);
275-
setCookie(tcJWTCookie, idToken, cookieExpireIn);
276-
setCookie(v3JWTCookie, idToken, cookieExpireIn);
277-
setCookie(tcSSOCookie, tcsso, cookieExpireIn);
277+
try {
278+
const exT = getCookieExpiry(idToken);
279+
if (exT) {
280+
setDomainCookie(tcJWTCookie, idToken, exT);
281+
setDomainCookie(v3JWTCookie, idToken, exT);
282+
setDomainCookie(tcSSOCookie, tcsso, exT);
283+
} else {
284+
setCookie(tcJWTCookie, idToken, cookieExpireIn);
285+
setCookie(v3JWTCookie, idToken, cookieExpireIn);
286+
setCookie(tcSSOCookie, tcsso, cookieExpireIn);
287+
}
288+
} catch (e) {
289+
logger('Error occured in fecthing token expiry time', e.message);
290+
}
291+
292+
// session still active, but app calling login
293+
if (!appUrl && returnAppUrl) {
294+
appUrl = returnAppUrl
295+
}
278296
redirectToApp();
279297
} else {
280298
logger("User active ? ", userActive);
@@ -457,10 +475,22 @@ const authSetup = function () {
457475
return false;
458476
});
459477
logger('Storing refreshed token...', true);
460-
setCookie(tcJWTCookie, idToken, cookieExpireIn);
461-
setCookie(v3JWTCookie, idToken, cookieExpireIn);
462-
setCookie(tcSSOCookie, tcsso, cookieExpireIn);
463-
informIt(success);
478+
try {
479+
const exT = getCookieExpiry(idToken);
480+
if (exT) {
481+
setDomainCookie(tcJWTCookie, idToken, exT);
482+
setDomainCookie(v3JWTCookie, idToken, exT);
483+
setDomainCookie(tcSSOCookie, tcsso, exT);
484+
} else {
485+
setCookie(tcJWTCookie, idToken, cookieExpireIn);
486+
setCookie(v3JWTCookie, idToken, cookieExpireIn);
487+
setCookie(tcSSOCookie, tcsso, cookieExpireIn);
488+
}
489+
informIt(success);
490+
} catch (e) {
491+
logger('Error occured in fecthing token expiry time', e.message);
492+
informIt(failed);
493+
}
464494
} else {
465495
logger("Refeshed token - user active ? ", userActive);
466496
informIt(failed);
@@ -546,6 +576,29 @@ const authSetup = function () {
546576
}
547577
}
548578

579+
function getCookieExpiry(token) {
580+
const d = getTokenExpirationDate(token)
581+
if (d === null) {
582+
return false;
583+
}
584+
const diff = d.valueOf() - (new Date().valueOf()); //in millseconds
585+
if (diff > 0) {
586+
return diff; // in milliseconds
587+
}
588+
return false;
589+
}
590+
591+
function setDomainCookie(cname, cvalue, exMilliSeconds) {
592+
const cdomain = getHostDomain();
593+
594+
let d = new Date();
595+
d.setTime(d.getTime() + exMilliSeconds);
596+
597+
let expires = ";expires=" + d.toUTCString();
598+
document.cookie = cname + "=" + cvalue + cdomain + expires + ";path=/";
599+
}
600+
601+
549602
// execute
550603
init();
551604
};

0 commit comments

Comments
 (0)