Clear local storage on logout

This commit is contained in:
Igor Zhukov 2016-06-07 21:32:27 +03:00
parent cf950efb84
commit 8bdebd543d
3 changed files with 75 additions and 17 deletions

View File

@ -140,11 +140,14 @@ Config.LangCountries = {"es": "ES", "ru": "RU", "en": "US", "de": "DE", "it": "I
return keyPrefix; return keyPrefix;
} }
function storageGetValue() { function storageGetValue(keys, callback) {
var keys = Array.prototype.slice.call(arguments), var single = false;
callback = keys.pop(), if (!Array.isArray(keys)) {
result = [], keys = Array.prototype.slice.call(arguments);
single = keys.length == 1, callback = keys.pop();
single = keys.length == 1;
}
var result = [],
value, value,
allFound = true, allFound = true,
prefix = storageGetPrefix(), prefix = storageGetPrefix(),
@ -227,14 +230,16 @@ Config.LangCountries = {"es": "ES", "ru": "RU", "en": "US", "de": "DE", "it": "I
chrome.storage.local.set(keyValues, callback); chrome.storage.local.set(keyValues, callback);
}; };
function storageRemoveValue () { function storageRemoveValue (keys, callback) {
var keys = Array.prototype.slice.call(arguments), if (!Array.isArray(keys)) {
prefix = storageGetPrefix(), keys = Array.prototype.slice.call(arguments);
i, key, callback; if (typeof keys[keys.length - 1] === 'function') {
callback = keys.pop();
if (typeof keys[keys.length - 1] === 'function') { }
callback = keys.pop();
} }
var prefix = storageGetPrefix(),
i, key;
for (i = 0; i < keys.length; i++) { for (i = 0; i < keys.length; i++) {
key = keys[i] = prefix + keys[i]; key = keys[i] = prefix + keys[i];
@ -255,12 +260,33 @@ Config.LangCountries = {"es": "ES", "ru": "RU", "en": "US", "de": "DE", "it": "I
} }
}; };
function storageClear(callback) {
if (useLs) {
try {
localStorage.clear();
} catch (e) {
useLs = false;
}
}
if (useCs) {
chrome.storage.local.clear(function () {
cache = {};
callback();
});
} else {
cache = {};
callback();
}
};
window.ConfigStorage = { window.ConfigStorage = {
prefix: storageSetPrefix, prefix: storageSetPrefix,
noPrefix: storageSetNoPrefix, noPrefix: storageSetNoPrefix,
get: storageGetValue, get: storageGetValue,
set: storageSetValue, set: storageSetValue,
remove: storageRemoveValue remove: storageRemoveValue,
clear: storageClear
}; };
})(this); })(this);

View File

@ -47,7 +47,7 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
for (var dcID = 1; dcID <= 5; dcID++) { for (var dcID = 1; dcID <= 5; dcID++) {
storageKeys.push('dc' + dcID + '_auth_key'); storageKeys.push('dc' + dcID + '_auth_key');
} }
return Storage.get.apply(Storage, storageKeys).then(function (storageResult) { return Storage.get(storageKeys).then(function (storageResult) {
var logoutPromises = []; var logoutPromises = [];
for (var i = 0; i < storageResult.length; i++) { for (var i = 0; i < storageResult.length; i++) {
if (storageResult[i]) { if (storageResult[i]) {
@ -58,12 +58,36 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
Storage.remove('dc', 'user_auth'); Storage.remove('dc', 'user_auth');
baseDcID = false; baseDcID = false;
telegramMeNotify(false); telegramMeNotify(false);
return mtpClearStorage();
}, function (error) { }, function (error) {
Storage.remove.apply(storageKeys); storageKeys.push('dc', 'user_auth');
Storage.remove('dc', 'user_auth'); Storage.remove(storageKeys);
baseDcID = false; baseDcID = false;
error.handled = true; error.handled = true;
telegramMeNotify(false); telegramMeNotify(false);
return mtpClearStorage();
});
});
}
function mtpClearStorage() {
var saveKeys = [];
for (var dcID = 1; dcID <= 5; dcID++) {
saveKeys.push('dc' + dcID + '_auth_key');
saveKeys.push('t_dc' + dcID + '_auth_key');
}
Storage.noPrefix();
Storage.get(saveKeys).then(function (values) {
Storage.clear().then(function () {
var restoreObj = {};
angular.forEach(saveKeys, function (key, i) {
var value = values[i];
if (value !== false && value !== undefined) {
restoreObj[key] = value;
}
});
Storage.noPrefix();
return Storage.set(restoreObj);
}); });
}); });
} }
@ -95,6 +119,9 @@ angular.module('izhukov.mtproto.wrapper', ['izhukov.utils', 'izhukov.mtproto'])
serverSaltHex = result[1]; serverSaltHex = result[1];
// console.log('ass', dcID, authKeyHex, serverSaltHex); // console.log('ass', dcID, authKeyHex, serverSaltHex);
if (authKeyHex && authKeyHex.length == 512) { if (authKeyHex && authKeyHex.length == 512) {
if (!serverSaltHex || serverSaltHex.length != 16) {
serverSaltHex = 'AAAAAAAAAAAAAAAA';
}
var authKey = bytesFromHex(authKeyHex); var authKey = bytesFromHex(authKeyHex);
var serverSalt = bytesFromHex(serverSaltHex); var serverSalt = bytesFromHex(serverSaltHex);

View File

@ -15,7 +15,7 @@ angular.module('izhukov.utils', [])
this.$get = ['$q', function ($q) { this.$get = ['$q', function ($q) {
var methods = {}; var methods = {};
angular.forEach(['get', 'set', 'remove'], function (methodName) { angular.forEach(['get', 'set', 'remove', 'clear'], function (methodName) {
methods[methodName] = function () { methods[methodName] = function () {
var deferred = $q.defer(), var deferred = $q.defer(),
args = Array.prototype.slice.call(arguments); args = Array.prototype.slice.call(arguments);
@ -28,6 +28,11 @@ angular.module('izhukov.utils', [])
return deferred.promise; return deferred.promise;
}; };
}); });
methods.noPrefix = function () {
ConfigStorage.noPrefix();
};
return methods; return methods;
}]; }];