Skip to content
This repository has been archived by the owner on Apr 15, 2019. It is now read-only.

Commit

Permalink
Handle invalid accounts in localStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
slaweet committed Nov 10, 2017
1 parent 4170b7b commit d30aa0c
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/utils/savedAccounts.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import { validateUrl } from './login';
import { extractAddress } from './api/account';

const isValidSavedAccount = ({ publicKey, network, address }) => {
try {
return extractAddress(publicKey) &&
network >= 0 && network <= 2 &&
(validateUrl(address).addressValidity === '' || network !== 2);
} catch (e) {
return false;
}
};

export const getSavedAccounts = () => {
const savedAccounts = localStorage.getItem('accounts');
if (savedAccounts) {
const accounts = JSON.parse(savedAccounts);
if (accounts instanceof Array) {
return accounts;
}
try {
return JSON.parse(localStorage.getItem('accounts')).filter(isValidSavedAccount);
} catch (e) {
return [];
}
return [];
};

export const getLastActiveAccount = () => (
Expand Down

0 comments on commit d30aa0c

Please sign in to comment.