-
-
Save rishichawda/e0be2787d0dddab4b56fdb17f05d3d89 to your computer and use it in GitHub Desktop.
Redux persistent storage encryption example.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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