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

CRDCDH-619 Switch from Session Storage to Cookies #220

Merged
merged 3 commits into from
Nov 22, 2023
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
2 changes: 1 addition & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = {
}
},
root: true,
ignorePatterns: ["injectEnv.js"],
ignorePatterns: ["public/injectEnv.js", "public/js/session.js"],
rules: {
// Note: you must disable the base rule as it can report incorrect errors
"react/jsx-filename-extension": [1, { extensions: [".js", ".jsx", ".tsx", ".ts"] }],
Expand Down
1 change: 1 addition & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@
-->
</body>
<script src="%PUBLIC_URL%/injectEnv.js"></script>
<script src="%PUBLIC_URL%/js/session.js"></script>
</html>
1 change: 0 additions & 1 deletion public/injectEnv.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable */
window.injectedEnv = {
NIH_AUTHORIZE_URL: '',
NIH_CLIENT_ID: '',
Expand Down
34 changes: 34 additions & 0 deletions public/js/session.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const sessionStorageTransfer = (event) => {
let $event = event;
if (!$event) {
$event = window.event;
} // ie suq
if (!$event.newValue) return; // do nothing if no value to work with
if ($event.key === 'getSessionStorage') {
// another tab asked for the sessionStorage -> send it
localStorage.setItem('sessionStorage', JSON.stringify(sessionStorage));
// the other tab should now have it, so we're done with it.
localStorage.removeItem('sessionStorage'); // <- could do short timeout as well.
} else if ($event.key === 'sessionStorage' && !sessionStorage.length) {
// another tab sent data <- get it
const data = JSON.parse($event.newValue);
Object.keys(data).forEach((key) => {
sessionStorage.setItem(key, data[key]);
});
} else if ($event.key === 'logout') {
sessionStorage.clear();
}
};

// listen for changes to localStorage
if (window.addEventListener) {
window.addEventListener('storage', sessionStorageTransfer, false);
} else {
window.addEventListener('onstorage', sessionStorageTransfer);
}

// Ask other tabs for session storage (this is ONLY to trigger 'storage' event)
if (!sessionStorage.length) {
localStorage.setItem('getSessionStorage', 'true');
localStorage.removeItem('getSessionStorage');
}