DEV Community

Discussion on: Please Stop Using Local Storage

Collapse
 
enijar profile image
James

If security is a concern, due to third party scripts having access to localStorage, then you can do something like this to remove third party access to it.

const Storage = {
  provider: window.localStorage,
  get(key) {
    return JSON.parse(this.provider.getItem(key))
  },
  set(key, value) {
    this.provider.setItem(key, JSON.stringify(value));
  },
  remove(key) {
    this.provider.removeItem(key);
  },
};
delete window.localStorage;

Storage.set("test", true);
Storage.get("test", true);
Storage.remove("test", true);

localStorage.getItem("test"); // will fail

This makes localStorage private to your Storage wrapper.