-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
66 additions
and
45 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,59 @@ | ||
import { map, last, uniq, set } from 'lodash' | ||
import { map, uniq, set } from 'lodash' | ||
import { storage } from './utils' | ||
import { get } from 'lodash/fp' | ||
|
||
// Storage keys | ||
/****************************************************** | ||
Helpers for saving/loading session info from storage | ||
*******************************************************/ | ||
|
||
// This string prefixes the key of everything we store | ||
const STORAGE_PREFIX = 'redux-sessions' | ||
|
||
function tokenStorageKey (userKey) { | ||
return STORAGE_PREFIX + ':token:' + userKey | ||
// Creates a storage key for a given user type and the data type being stored. | ||
// E.g. ('user', 'token') -> 'redux-sessions:user:token' | ||
function serializeStorageKey (userType, dataType) { | ||
return [ STORAGE_PREFIX, dataType, userType ].join(':') | ||
} | ||
|
||
function persistStorageKey (userKey) { | ||
return STORAGE_PREFIX + ':persist:' + userKey | ||
// Splits a storage key into its parts so we can retrieve info about what's being stored. | ||
function deserializeStorageKey (storageKey) { | ||
// eslint-disable-next-line | ||
const [ _, dataType, userType ] = storageKey.split(':') | ||
return { dataType, userType } | ||
} | ||
|
||
function getUserKeyFromStorageKey (storageKey) { | ||
return last(storageKey.split(':')) | ||
// Given a storage key, returns the user type. | ||
function getUserTypeFromStorageKey (storageKey) { | ||
const { userType } = deserializeStorageKey(storageKey) | ||
return userType | ||
} | ||
|
||
// Storage helpers | ||
// Returns the storage key for storing a user token. | ||
function tokenStorageKey (userType) { | ||
return serializeStorageKey(userType, 'token') | ||
} | ||
|
||
// Returns the storage key for storing a user's persistence. | ||
function persistStorageKey (userType) { | ||
return serializeStorageKey(userType, 'persist') | ||
} | ||
|
||
// Loads the redux state from local / session storage. | ||
// We use the storage prefix to figure out which values we've saved. | ||
export function loadSessionState () { | ||
const userKeys = uniq(storage.getAllKeys() | ||
.filter(key => key.startsWith(STORAGE_PREFIX)) | ||
.map(getUserKeyFromStorageKey)) | ||
const storageKeys = storage.getAllKeys().filter(key => key.startsWith(STORAGE_PREFIX)) | ||
const userTypes = uniq(storageKeys.map(getUserTypeFromStorageKey)) | ||
const state = {} | ||
userKeys.forEach(userKey => set(state, userKey, { | ||
token: storage.getItem(tokenStorageKey(userKey)), | ||
persist: !!storage.getItem(persistStorageKey(userKey)), | ||
userTypes.forEach(userType => set(state, userType, { | ||
token: storage.getItem(tokenStorageKey(userType)), | ||
persist: !!storage.getItem(persistStorageKey(userType)), | ||
})) | ||
return state | ||
} | ||
|
||
// Saves the redux state to local / session storage. | ||
export function saveSessionState (state) { | ||
return map(state, ({ token, persist }, userKey) => { | ||
storage.setItem(tokenStorageKey(userKey), token, { persist }) | ||
storage.setItem(persistStorageKey(userKey), persist, { persist }) | ||
return map(state, ({ token, persist }, userType) => { | ||
storage.setItem(tokenStorageKey(userType), token, { persist }) | ||
storage.setItem(persistStorageKey(userType), persist, { persist }) | ||
}) | ||
} |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import Storage from 'dom-storage' | ||
|
||
// This file will be run before each individual test file. | ||
// Here we use it to set up some storage mocks. | ||
// Here we're using it to set up some storage mocks. | ||
|
||
global.localStorage = new Storage(null, { strict: true }) | ||
global.sessionStorage = new Storage(null, { strict: true }) |