Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for when localStorage is available but access is restricted #3031

Merged
merged 4 commits into from
Sep 11, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 35 additions & 1 deletion packages/web3-eth-accounts/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -525,10 +525,44 @@ Wallet.prototype.load = function (password, keyName) {
return this.decrypt(keystore || [], password);
};

if (typeof localStorage === 'undefined') {
if (!storageAvailable('localStorage')) {
delete Wallet.prototype.save;
delete Wallet.prototype.load;
}

/**
* Checks whether a storage type is available or not
* For more info on how this works, please refer to MDN documentation
* https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API#Feature-detecting_localStorage
*
* @method storageAvailable
* @param {String} type the type of storage ('localStorage', 'sessionStorage')
* @returns {Boolean} a boolean indicating whether the specified storage is available or not
*/
function storageAvailable(type) {
var storage;
try {
storage = window[type];
var x = '__storage_test__';
storage.setItem(x, x);
storage.removeItem(x);
return true;
}
catch(e) {
return e && (
chaitanyapotti marked this conversation as resolved.
Show resolved Hide resolved
// everything except Firefox
e.code === 22 ||
// Firefox
e.code === 1014 ||
// test name field too, because code might not be present
// everything except Firefox
e.name === 'QuotaExceededError' ||
// Firefox
e.name === 'NS_ERROR_DOM_QUOTA_REACHED') &&
// acknowledge QuotaExceededError only if there's something already stored
(storage && storage.length !== 0);
}
}


module.exports = Accounts;