From abd4ccac1e980c42d854ab29bf5aff8a1e6dce3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Guti=C3=A9rrez?= Date: Wed, 8 May 2019 12:29:32 +0200 Subject: [PATCH] Add a error message if localStorage is not available. --- integration/ocean/AuthenticationToken.test.ts | 4 +++- src/ocean/OceanAuth.ts | 18 +++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/integration/ocean/AuthenticationToken.test.ts b/integration/ocean/AuthenticationToken.test.ts index 47324e9..f269671 100644 --- a/integration/ocean/AuthenticationToken.test.ts +++ b/integration/ocean/AuthenticationToken.test.ts @@ -11,7 +11,9 @@ describe("Authentication Token", () => { let account2: Account before(async () => { - localStorage.clear() + try { + localStorage.clear() + } catch { } ocean = await Ocean.getInstance(config) diff --git a/src/ocean/OceanAuth.ts b/src/ocean/OceanAuth.ts index 1acd9f8..316cd3f 100644 --- a/src/ocean/OceanAuth.ts +++ b/src/ocean/OceanAuth.ts @@ -79,7 +79,12 @@ export class OceanAuth extends Instantiable { * @param {Account} account Signer account. */ public async restore(account: Account): Promise { - const token = this.readToken(account.getId()) + let token + try { + token = this.readToken(account.getId()) + } catch { + return + } if (!token) { return } @@ -100,6 +105,7 @@ export class OceanAuth extends Instantiable { } private writeToken(address: string, token: string) { + const localStorage = this.getLocalStorage() const storedTokens = localStorage.getItem(localStorageKey) const tokens = storedTokens ? JSON.parse(storedTokens) : {} @@ -110,9 +116,19 @@ export class OceanAuth extends Instantiable { } private readToken(address: string) { + const localStorage = this.getLocalStorage() const storedTokens = localStorage.getItem(localStorageKey) const tokens = storedTokens ? JSON.parse(storedTokens) : {} return tokens[address] } + + private getLocalStorage() { + try { + localStorage.getItem("") + } catch { + throw new Error("LocalStorage is not supported. This feature is only available on browsers.") + } + return localStorage + } }