Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does not decode the encoded String stored in Localstorage #34

Open
RayyanNafees opened this issue May 10, 2023 · 1 comment
Open

Does not decode the encoded String stored in Localstorage #34

RayyanNafees opened this issue May 10, 2023 · 1 comment

Comments

@RayyanNafees
Copy link

RayyanNafees commented May 10, 2023

I used crypto-js to encode & decode my data before saving via persistenAtom

Code

import { persistentAtom } from "@nanostores/persistent";
import CryptoJS from 'crypto-js'

const ENCRYPTION_KEY = 'my-secret-key'

const encrypt = (data: any):string => CryptoJS.AES.encrypt(JSON.stringify(data), ENCRYPTION_KEY).toString()


const decrypt = (encryptedData:string):any => {
  const bytes = CryptoJS.AES.decrypt(encryptedData, ENCRYPTION_KEY)

return JSON.parse(bytes.toString(CryptoJS.enc.Utf8))

}

export const counterP = persistentAtom<number>("counter", 0, {
  encode: encrypt,
  decode: decrypt,
});

export const add = action(counterP, "add", (store) => {
  counterP.set(counterP.get() + 1);
});

Issue

Whenever i load the stored data using counterP.get() it shows me the encrypted data (not the actual data after decoding it as it was supposed to)

Resolve

decode the data stored in localStorage before loading it into the Atom Store

Would love if that custom storage issue gets resolved as described here #30

@ai
Copy link
Member

ai commented May 11, 2023

decode the data stored in localStorage before loading it into the Atom Store

Could you show an idea in PR (you can make a draft PR without tests)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants