Skip to content

Commit ec05c06

Browse files
author
sachin-maheshwari
authored
Merge pull request #104 from topcoder-platform/dev
Cookie expiry time changes and distinguish between signup and signin.
2 parents ae8be61 + 6afc604 commit ec05c06

File tree

2 files changed

+92
-15
lines changed

2 files changed

+92
-15
lines changed

src/connector-wrapper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const proxyCall = function () {
4949
if (safeFormat) {
5050
window.removeEventListener('message', receiveMessage)
5151
if (e.data.type === "SUCCESS") {
52-
token ? resolve({ token: token }) : reject("v3jwt cookie not found")
52+
getToken('v3jwt') ? resolve({ token: getToken('v3jwt') }) : reject("v3jwt cookie not found")
5353
} else {
5454
reject("unable to get refesh token")
5555
}

web-assets/js/setupAuth0WithRedirect.js

Lines changed: 91 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ const qs = (function (a) {
2323

2424
const authSetup = function () {
2525

26-
let domain = 'auth.topcoder.com';
27-
const clientId = 'UW7BhsnmAQh0itl56g1jUPisBO9GoowD';
26+
let domain = 'auth.topcoder-dev.com';
27+
const clientId = 'BXWXUWnilVUPdN01t2Se29Tw2ZYNGZvH';
2828
const useLocalStorage = false;
2929
const useRefreshTokens = false;
3030
const v3JWTCookie = 'v3jwt';
@@ -38,16 +38,17 @@ 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;
42-
const loggerMode = "prod";
41+
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') &&
4950
(enterpriseCustomers.indexOf(utmSource) > -1)) {
50-
domain = "topcoder.auth0.com";
51+
domain = "topcoder-dev.auth0.com";
5152
returnAppUrl += '&utm_source=' + utmSource;
5253
}
5354

@@ -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);
@@ -485,7 +515,31 @@ const authSetup = function () {
485515
if (token && !isTokenExpired(token)) {
486516
informIt(success);
487517
} else if (!token) {
488-
informIt(failed);
518+
const auth0Session = getCookie('auth0.is.authenticated');
519+
logger('auth0 session available ?', auth0Session);
520+
if (auth0Session) {
521+
logger('auth session true', 1);
522+
if (!auth0) {
523+
createAuth0Client({
524+
domain: domain,
525+
client_id: clientId,
526+
cacheLocation: useLocalStorage
527+
? 'localstorage'
528+
: 'memory',
529+
useRefreshTokens: useRefreshTokens
530+
}).then(function (newAuth0Obj) {
531+
getToken(newAuth0Obj);
532+
}).catch(function (e) {
533+
logger("Error occurred in re-initializing auth0 object: ", e);
534+
informIt(failed);
535+
});
536+
} else {
537+
getToken(auth0);
538+
}
539+
} else {
540+
informIt(failed);
541+
}
542+
489543
} else {
490544
if (auth0) {
491545
getToken(auth0);
@@ -546,6 +600,29 @@ const authSetup = function () {
546600
}
547601
}
548602

603+
function getCookieExpiry(token) {
604+
const d = getTokenExpirationDate(token)
605+
if (d === null) {
606+
return false;
607+
}
608+
const diff = d.valueOf() - (new Date().valueOf()); //in millseconds
609+
if (diff > 0) {
610+
return diff; // in milliseconds
611+
}
612+
return false;
613+
}
614+
615+
function setDomainCookie(cname, cvalue, exMilliSeconds) {
616+
const cdomain = getHostDomain();
617+
618+
let d = new Date();
619+
d.setTime(d.getTime() + exMilliSeconds);
620+
621+
let expires = ";expires=" + d.toUTCString();
622+
document.cookie = cname + "=" + cvalue + cdomain + expires + ";path=/";
623+
}
624+
625+
549626
// execute
550627
init();
551628
};

0 commit comments

Comments
 (0)