softvar / secure-ls

:lock: Secure localStorage data with high level of encryption and data compression
http://softvar.github.io/secure-ls
MIT License
644 stars 81 forks source link

need secure-ls support sessionStorage #32

Closed ayun2001 closed 3 months ago

ayun2001 commented 4 years ago

Hi Varun Malhotra, do you have any planning to let secure-ls support sessionStorage?

YanDevDe commented 4 years ago

From other fork, someone already tried it: https://github.com/hansanwok/secure-ls/pull/2 It may need to pull request here too.

GsHeri commented 4 years ago

i need it too. will you merge it? if not, would you mind explaining me how i can install the version with the PR integrated via npm? i've never done this before

ghost commented 4 years ago

any news about this?

gilbertg commented 2 years ago

A hacky TS solution

export class SecureStorage extends SecureLS {

    constructor(config: { isCompression?: boolean, encodingType?: string, encryptionSecret?: string, encryptionNamespace?: string, useSessionStore?: boolean }) {
        super(config);

        // tslint:disable-next-line:ban-ts-ignore
        // @ts-ignore
        super.ls = config.useSessionStore ? sessionStorage : localStorage;
    }
}
softvar commented 3 months ago

Hey @ayun2001 , @YanDevDe , @GsHeri , @gilbertg ,

I refactored the code & tests have published a new version 2.0.0 The latest version has support for sessionStorage as well as any kind of custom storage.

You can refer this - https://github.com/softvar/secure-ls?tab=readme-ov-file#api-documentation

window.customSecureLsStore = {};
const storage = {
  setItem: (key, value) => {
    window.customSecureLsStore[key] = value || '';
  },
  getItem: (key) => {
    return window.customSecureLsStore[key] || null;
  },
  removeItem: (key) => {
    delete window.customSecureLsStore[key];
  },
  clear: () => {
    window.customSecureLsStore = {};
  },
};

const ls = new SecureLS({ encodingType: 'aes', isCompression: true, storage: storage });

I hope you will be glad to know this 🙌