Skip to content

Instantly share code, notes, and snippets.

@giubueno
Created November 30, 2018 22:59
Show Gist options
  • Save giubueno/17b75d572838daf9a7c6b912693b8150 to your computer and use it in GitHub Desktop.
Save giubueno/17b75d572838daf9a7c6b912693b8150 to your computer and use it in GitHub Desktop.
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