Skip to content

Instantly share code, notes, and snippets.

@rishichawda
Last active September 20, 2018 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rishichawda/e0be2787d0dddab4b56fdb17f05d3d89 to your computer and use it in GitHub Desktop.
Save rishichawda/e0be2787d0dddab4b56fdb17f05d3d89 to your computer and use it in GitHub Desktop.
Redux persistent storage encryption example.
import { combineReducers, createStore, applyMiddleware } from 'redux';
import storage from 'redux-persist/lib/storage';
import { persistReducer, persistStore } from 'redux-persist';
import promise from 'redux-promise';
import createEncryptor from 'redux-persist-transform-encrypt'; // <----- Import createEncryptor
const rootReducer = combineReducers({
myPersistentData: dataReducer,
someMorePresistentData: randomReducer,
});
/*
Configure your encryptor with the secret key
NOTE: This secret key is required for access to data.
If the key is incorrect or changed after storing data
with some different key, it will return null.
*/
const encryptor = createEncryptor({
secretKey: 'my-not-so-secret-encryption-key',
onError: (error) => {
console.warn('error occured on encryptor', error);
},
});
const config = {
key: '_myPersistentDataKey',
storage,
/* Add the encryptor as a transform property. This is will
tell the persistent storage to encrypt data using the
configuration given earlier,i.e., our secretKey. */
transforms: [encryptor], // <----- Add this line.
};
const reducer = persistReducer(config, rootReducer);
const store = createStore(
reducer,
{},
applyMiddleware(promise),
);
const persist = persistStore(store);
export default { store, persist };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment