Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(header): remove persisted-state data from session-storage #4517

Merged
merged 4 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading