sushinpv / react-secure-storage

This is a wrapper written above local storage to write the data securely to local storage
https://npmjs.com/package/react-secure-storage
MIT License
126 stars 12 forks source link

Added sessionStorage support #49

Open Anon0x19 opened 2 months ago

Anon0x19 commented 2 months ago

I was anticipating the merge of pull/40. However, since it hasn't been merged yet, I thought the PR might have been discontinued. Given that others are also interested in this feature, I decided to implement it myself.

Previously, I created a PR but I noticed a few issues when integrating it into my own project. As a result, I decided to close that PR and have now raised this one with the necessary changes to ensure it works properly.

I hope we can get this feature merged soon, as it would be quite useful. If there are any further changes needed, please do let me know, as I am more than happy to assist with further contributions.

sushinpv commented 2 months ago

Hi @Anon0x19 the only issue we faced is on the NextJs when the component is rendering on the server side! We are working on it, and we will be closing this Request with this week without any delay

Anon0x19 commented 2 months ago

Hi @Anon0x19 the only issue we faced is on the NextJs when the component is rendering on the server side! We are working on it, and we will be closing this Request with this week without any delay

Thank you for the quick response @sushinpv! This fix should work on NextJs. I just tested it using nextjs-react-secure-storage-example. The only change I made was to modify the import statement from import secureLocalStorage from "react-secure-storage"; to import {secureLocalStorage} from "react-secure-storage";. Additionally, I used yarn link and yarn link react-secure-storage with the branch in this PR

sushinpv commented 2 months ago

Thanks a lot @Anon0x19 , Let me go through all the test cases and if everything looks good we will be able to release this new version within this week

Anon0x19 commented 2 months ago

Thanks a lot @Anon0x19 , Let me go through all the test cases and if everything looks good we will be able to release this new version within this week

Awesome! Thank you again for getting back to me so quickly. You were right about the issue with certain Next.js versions. I tested it using 14.2.5 and 13.4.13 and it seems to work on both without issues now.

By the way, for react, I had to change the usage from i.e secureLocalStorage.setItem("number", 12); to secureLocalStorage?.setItem("number", 12); to avoid errors.

Furthermore, it seems the issue arose from new secureStorage(localStorage), but I've now changed it to typeof window !== 'undefined' && typeof window.localStorage !== 'undefined' ? new secureStorage(window.localStorage) : null;

Thank you for your time and for looking into this and testing it further.

sushinpv commented 2 months ago

This will be breaking change for the existing users, and integrations, I'll make the necessary changes and planning to close this in next two days