redux-persist-transform-encrypt icon indicating copy to clipboard operation
redux-persist-transform-encrypt copied to clipboard

persistor.purge() after error

Open mattrabe opened this issue 5 years ago • 0 comments

When decryption fails (which triggers the onError event handler) the data in the resulting store may not be formed as expected. The store will NOT be automatically reverted to initialState. I do not suggest that it automatically be reverted, but rather I suggest an addition to documentation.

Since people commonly rely on initialState "guaranteeing" a certain shape for the store, when decryption fails this will result in errors such as "Cannot read property 'foo' of undefined" or "Cannot destructure property 'foo' of 'bar' as it is null". This can happen if you're using a cookie or other expiring token as secret key, and when that token expires, this will result in the the app crashing!

There is a simple solution to this, but I could not find it documented anywhere:

  onError: error => {
    persistor.purge()
  },

This will revert the store to initialState, and occurs before selectors, etc - so you will not get the error or crash.

I suggest that the above be added to the readme, to provide an obvious example of how to reset state and avoid runtime errors. I'd be happy to open a PR for this. I hope this helps someone!

mattrabe avatar Feb 27 '20 21:02 mattrabe