Add NKeyStorage class to retrieve and set keys in browser storage in a mostly-secure way
This commit is contained in:
15
src/utils/storage.ts
Normal file
15
src/utils/storage.ts
Normal 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 };
|
||||
Reference in New Issue
Block a user