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:
parent
4d73eb8697
commit
abd4ccac1e
@ -11,7 +11,9 @@ describe("Authentication Token", () => {
|
||||
let account2: Account
|
||||
|
||||
before(async () => {
|
||||
try {
|
||||
localStorage.clear()
|
||||
} catch { }
|
||||
|
||||
ocean = await Ocean.getInstance(config)
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user