polemius / recoil-persist

Package for recoil state manager to persist and rehydrate store
https://polemius.dev/recoil-persist/
MIT License
348 stars 40 forks source link

Cannot Encryption localStorage key #49

Open bobwatcherx opened 2 years ago

bobwatcherx commented 2 years ago

i want to encrypt localstorage key and value

i using encrypt-storage but not work , i reload page , the last state to defaul t again

polemius commented 2 years ago

You could pass Storage implementation to recoilPersist as storage, and change behavior of getItem and setItem. For example this storage encode everything in base64 and save it in localstorage.

import { encode, decode } from 'js-base64';

const localStorageBase64 = () => {
  return {
    setItem: (key, value) => {
      localStorage.setItem(encode(key), encode(value))
    },
    getItem: (key) => {
      const a =  localStorage.getItem(encode(key))
      return decode(a || '')
    },
    clear: () => {
      localStorage.clear()
    },
  }
}

const { persistAtom } = recoilPersist({ key: 'abc1234', storage: localStorageBase64() })