recoil-persist icon indicating copy to clipboard operation
recoil-persist copied to clipboard

Cannot Encryption localStorage key

Open bobwatcherx opened this issue 2 years ago • 1 comments

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

bobwatcherx avatar Oct 24 '21 02:10 bobwatcherx

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() })

polemius avatar Oct 26 '21 06:10 polemius