Skip to content

Commit

Permalink
chore(header): remove persisted-state data from session-storage (#4517)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverschuerch authored Jan 29, 2025
1 parent c5b9284 commit de57604
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 90 deletions.
6 changes: 6 additions & 0 deletions .changeset/short-olives-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@swisspost/internet-header': patch
---

Replaced `sessionData` and `address` properties in the sessionStorage item `klp.widget.state`.
If you're looking for this info, contact the Swiss Post Design System Team!
96 changes: 6 additions & 90 deletions packages/internet-header/src/assets/js/klp-login-widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -650,60 +650,16 @@ const vertx = window.vertx || {};
isUserActive = true;
}

function restoreState() {
const state = loadPersistedState();
if (state) {
address = state.address;
retrySubscribeOnFail = true;
sessionData = state.sessionData;
renderWidget();
if (isCurrentLocationPostCh()) {
renderNotificationsWidget(loadDocumentFromCache(documentUnreadNotifications));
}
}
}

function loadPersistedState() {
if (isHTML5StorageSupported()) {
const persistedState = sessionStorage.getItem(persistedStateKey);
if (persistedState) {
try {
const state = JSON.parse(persistedState);
if (state.ttl > new Date().getTime() && isPersistedStateValid(state.sessionData)) {
log('Valid persisted state loaded: ' + persistedState);
return state;
} else {
log('Persisted state expired or invalid');
removePersistedState();
setControlCookie('all', 'sub');
}
} catch (err) {
log("Persisted state was invalid due to error '" + err + "'");
removePersistedState();
setControlCookie('all', 'sub');
}
} else {
log('No persisted state found');
removePersistedState();
setControlCookie('all', 'sub');
}
} else {
log('State not loaded because HTML storage not supported');
}
return null;
}

function persistState(ttl) {
if (isHTML5StorageSupported()) {
sessionStorage.setItem(
persistedStateKey,
'{"ttl": ' +
(new Date().getTime() + ttl) +
',"sessionData": ' +
JSON.stringify(sessionData) +
', "address":"' +
address +
'"}',
JSON.stringify({
ttl: new Date().getTime() + ttl,
sessionData:
"If you're looking for this info, contact the Swiss Post Design System Team!",
address: "If you're looking for this info, contact the Swiss Post Design System Team!",
}),
);
log('State persisted');
setControlCookie('hash', encodeURIComponent(hash(sessionData)));
Expand All @@ -722,32 +678,6 @@ const vertx = window.vertx || {};
}
}

function isPersistedStateValid(persistedData) {
const hashPersistedData = hash(persistedData).toString();
const hashCookie = getControlCookieVal('hash');
if (hashPersistedData === hashCookie) {
return true;
}
if (hashCookie === undefined && !isCurrentLocationPostCh()) {
log(
'Cache validated because on a different host=[' +
window.location.hostname +
'] than control cookie domain=[' +
controlCookieDomain +
']',
);
return true;
}
log(
'PersistedData are invalid [hashPersistedData=' +
hashPersistedData +
',control cookie=' +
hashCookie +
']',
);
return false;
}

function isCurrentLocationPostCh() {
return controlCookieDomainRegEx.test(window.location.hostname);
}
Expand Down Expand Up @@ -845,19 +775,6 @@ const vertx = window.vertx || {};
}
}

function loadDocumentFromCache(documentType) {
const persistedKey = persistedDocumentPrefix + documentType;
if (isHTML5StorageSupported()) {
const persistedDocument = sessionStorage.getItem(persistedKey);
if (persistedDocument) {
const document = $.parseJSON(persistedDocument);
log('Document ' + documentType + ' has been read from cache with value ' + document);
return document;
}
}
return null;
}

function saveDocumentOnCache(document, documentType) {
const key = persistedDocumentPrefix + documentType;
if (isHTML5StorageSupported()) {
Expand Down Expand Up @@ -1517,7 +1434,6 @@ const vertx = window.vertx || {};
}

function init() {
restoreState();
subscribe();
if (conf.keepAliveOnInit && isUserAuthenticated()) {
keepAliveSessionsOnInit();
Expand Down

0 comments on commit de57604

Please sign in to comment.