Created
November 30, 2018 22:59
-
-
Save giubueno/17b75d572838daf9a7c6b912693b8150 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 { fromJS } from 'immutable'; | |
import sjcl from 'sjcl'; | |
/* | |
* This function gets the encrypted object from the session storage, | |
* decrypt it and set the state back into the store. | |
*/ | |
export function recoverStateFromSession(key, state, password) { | |
const encryptedData = JSON.parse(sessionStorage.getItem(key)); | |
if(!encryptedData) { return state; } | |
const json = sjcl.decrypt(password, encryptedData); | |
const data = JSON.parse(json); | |
return state.set(key, fromJS(data)); | |
} | |
/* | |
* Saves the state locally in session storage. | |
*/ | |
export function saveStateInSession(key, data, password) { | |
const encryptedData = sjcl.encrypt(password, JSON.stringify(data)); | |
sessionStorage.setItem(key, JSON.stringify(encryptedData)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment