Add NKeyStorage class to retrieve and set keys in browser storage in a mostly-secure way

This commit is contained in:
Alex Gleason
2024-02-18 16:43:15 -06:00
parent b382a96d6a
commit 15ae362a8e
7 changed files with 159 additions and 5 deletions

15
src/utils/storage.ts Normal file
View File

@@ -0,0 +1,15 @@
/** Lock a key from being accessed by `localStorage` and `sessionStorage`. */
function lockStorageKey(key: string): void {
const proto = Object.getPrototypeOf(localStorage ?? sessionStorage);
const _getItem = proto.getItem;
proto.getItem = function(_key: string) {
if (_key === key) {
throw new Error(`${_key} is locked`);
} else {
return _getItem.bind(this)(_key);
}
};
}
export { lockStorageKey };