diff --git a/www/advanced-http.js b/www/advanced-http.js index 8d83ecb..54e5026 100644 --- a/www/advanced-http.js +++ b/www/advanced-http.js @@ -106,7 +106,10 @@ var http = { this.headers[header] = value; }, setDataSerializer: function (serializer) { - this.dataSerializer = checkSerializer(serializer); + this.dataSerializer = checkSerializer(serializer); + }, + clearCookies: function () { + return cookieHandler.clearCookies(); }, enableSSLPinning: function (enable, success, failure) { return exec(success, failure, 'CordovaHttpPlugin', 'enableSSLPinning', [enable]); diff --git a/www/angular-integration.js b/www/angular-integration.js index 9e897d2..c903b91 100644 --- a/www/angular-integration.js +++ b/www/angular-integration.js @@ -44,6 +44,9 @@ function registerService(http) { setDataSerializer: function (serializer) { return http.setParamSerializer(serializer); }, + clearCookies: function () { + return http.clearCookies(); + }, enableSSLPinning: function (enable) { return makePromise(http.enableSSLPinning, [enable]); }, diff --git a/www/cookie-handler.js b/www/cookie-handler.js index a4e3ad9..5aa0dc1 100644 --- a/www/cookie-handler.js +++ b/www/cookie-handler.js @@ -1,12 +1,17 @@ var pluginId = module.id.slice(0, module.id.indexOf('.')); var ToughCookie = require(pluginId + '.tough-cookie'); var WebStorageCookieStore = require(pluginId + '.local-storage-store'); -var store = new WebStorageCookieStore(); + +var storage = window.localStorage; +var storeKey = '__advancedHttpCookieStore__'; + +var store = new WebStorageCookieStore(storage, storeKey); var cookieJar = new ToughCookie.CookieJar(store); module.exports = { setCookie: setCookie, - getCookie: getCookie + getCookie: getCookie, + clearCookies: clearCookies } function setCookie(url, cookieStr) { @@ -17,3 +22,7 @@ function setCookie(url, cookieStr) { function getCookie(url) { return cookieJar.getCookieStringSync(url); } + +function clearCookies() { + window.localStorage.removeItem(storeKey); +} diff --git a/www/local-storage-store.js b/www/local-storage-store.js index 76d686c..353d820 100644 --- a/www/local-storage-store.js +++ b/www/local-storage-store.js @@ -34,13 +34,10 @@ var pluginId = module.id.slice(0, module.id.indexOf('.')); var ToughCookie = require(pluginId + '.tough-cookie'); var _ = require(pluginId + '.lodash'); -var Cookie = ToughCookie.Cookie; - -var STORE_KEY = '__cookieStore__'; - -function WebStorageCookieStore(storage) { +function WebStorageCookieStore(storage, storeKey) { ToughCookie.Store.call(this); this._storage = storage || window.localStorage; + this._storeKey = storeKey || '__cookieStore__'; this.synchronous = true; } @@ -50,7 +47,7 @@ WebStorageCookieStore.prototype.findCookie = function(domain, path, key, callbac var store = this._readStore(); var cookie = _.get(store, [domain, path, key], null); - callback(null, Cookie.fromJSON(cookie)); + callback(null, ToughCookie.Cookie.fromJSON(cookie)); }; WebStorageCookieStore.prototype.findCookies = function(domain, path, callback) { @@ -83,7 +80,7 @@ WebStorageCookieStore.prototype.findCookies = function(domain, path, callback) { }); cookies = cookies.map(function(cookie) { - return Cookie.fromJSON(cookie); + return ToughCookie.Cookie.fromJSON(cookie); }); callback(null, cookies); @@ -157,7 +154,7 @@ WebStorageCookieStore.prototype.getAllCookies = function(callback) { }); cookies = cookies.map(function(cookie) { - Cookie.fromJSON(cookie) + return ToughCookie.Cookie.fromJSON(cookie); }); cookies.sort(function(c1, c2) { @@ -168,7 +165,7 @@ WebStorageCookieStore.prototype.getAllCookies = function(callback) { }; WebStorageCookieStore.prototype._readStore = function() { - var json = this._storage.getItem(STORE_KEY); + var json = this._storage.getItem(this._storeKey); if (json !== null) { try { @@ -180,7 +177,7 @@ WebStorageCookieStore.prototype._readStore = function() { }; WebStorageCookieStore.prototype._writeStore = function(store) { - this._storage.setItem(STORE_KEY, JSON.stringify(store)); + this._storage.setItem(this._storeKey, JSON.stringify(store)); }; module.exports = WebStorageCookieStore;