1
0
mirror of https://github.com/oceanprotocol-archive/squid-js.git synced 2024-02-02 15:31:51 +01:00

Add a error message if localStorage is not available.

This commit is contained in:
Pedro Gutiérrez 2019-05-08 12:29:32 +02:00
parent 4d73eb8697
commit abd4ccac1e
2 changed files with 20 additions and 2 deletions

View File

@ -11,7 +11,9 @@ describe("Authentication Token", () => {
let account2: Account
before(async () => {
localStorage.clear()
try {
localStorage.clear()
} catch { }
ocean = await Ocean.getInstance(config)

View File

@ -79,7 +79,12 @@ export class OceanAuth extends Instantiable {
* @param {Account} account Signer account.
*/
public async restore(account: Account): Promise<string> {
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
}
}