redux-persist-cookie-storage
Advanced tools
Comparing version 0.1.1 to 0.2.0
{ | ||
"name": "redux-persist-cookie-storage", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "redux-persist storage adapter for cookies", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha test" | ||
"test": "mocha test --timeout 15000" | ||
}, | ||
@@ -9,0 +9,0 @@ "author": "Oliver Spindler <os@oliverspindler.com>", |
@@ -20,5 +20,26 @@ # Redux Persist Cookie Storage Adapter | ||
const store = createStore(reducer, undefined, autoRehydrate()) | ||
// By default, session cookies are used | ||
persistStore(store, { storage: new CookieStorage() }) | ||
// Expiration time can be set via options | ||
persistStore(store, { storage: new CookieStorage({ | ||
expiration: { | ||
'default': 365 * 86400 // Cookies expire after one year | ||
} | ||
}) | ||
}) | ||
// Default expiration time can be overridden for specific parts of the store: | ||
persistStore(store, { storage: new CookieStorage({ | ||
expiration: { | ||
'default': null, // Session cookies used by default | ||
'storeKey': 600 // State in key `storeKey` expires after 10 minutes | ||
} | ||
}) | ||
}) | ||
``` | ||
### Server | ||
@@ -39,4 +60,7 @@ | ||
}) | ||
``` | ||
N.B.: Custom expiration times are not supported server-side at the moment. | ||
## Development | ||
@@ -43,0 +67,0 @@ |
@@ -9,2 +9,6 @@ var Cookies = require('cookies-js'); | ||
this.indexKey = options.indexKey || 'reduxPersistIndex'; | ||
this.expiration = options.expiration || {}; | ||
if (!this.expiration.default) { | ||
this.expiration.default = null; | ||
} | ||
@@ -25,8 +29,25 @@ if (options.windowRef) { | ||
CookieStorage.prototype.setItem = function (key, value, callback) { | ||
this.cookies.set(this.keyPrefix + key, value); | ||
var options = {}; | ||
var expires = this.expiration.default; | ||
if (typeof this.expiration[key] !== 'undefined') { | ||
expires = this.expiration[key] | ||
} | ||
if (expires) { | ||
options["expires"] = expires; | ||
} | ||
this.cookies.set(this.keyPrefix + key, value, options); | ||
// Update key index | ||
var indexOptions = {}; | ||
if (this.expiration.default) { | ||
indexOptions["expires"] = this.expiration.default; | ||
} | ||
this.getAllKeys(function (error, allKeys) { | ||
if (allKeys.indexOf(key) === -1) { | ||
allKeys.push(key); | ||
this.cookies.set(this.indexKey, JSON.stringify(allKeys)); | ||
this.cookies.set(this.indexKey, JSON.stringify(allKeys), indexOptions); | ||
} | ||
@@ -33,0 +54,0 @@ callback(null); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4920
81
69