From 1f83e333cb1f6637990d58e4840001b4697618cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Guti=C3=A9rrez?= Date: Thu, 9 May 2019 16:34:44 +0200 Subject: [PATCH] Improve how can be get the token from the account. --- integration/ocean/AuthenticationToken.test.ts | 7 +++++++ src/ocean/Account.ts | 14 +++++++++++--- src/ocean/OceanAccounts.ts | 7 ------- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/integration/ocean/AuthenticationToken.test.ts b/integration/ocean/AuthenticationToken.test.ts index f269671..5cc6e9f 100644 --- a/integration/ocean/AuthenticationToken.test.ts +++ b/integration/ocean/AuthenticationToken.test.ts @@ -38,7 +38,11 @@ describe("Authentication Token", () => { }) it("should store the token for a user", async () => { + assert.isUndefined(await account1.getToken()) + await ocean.auth.store(account1) + + assert.match(await account1.getToken(), /^0x[a-f0-9]{130}-[0-9]{0,14}/i) }) it("should restore the token for a user", async () => { @@ -60,11 +64,14 @@ describe("Authentication Token", () => { acc2Stored = await ocean.auth.isStored(account2) assert.isTrue(acc1Stored) + assert.isTrue(await account1.isTokenStored()) assert.isFalse(acc2Stored) + assert.isFalse(await account2.isTokenStored()) await ocean.auth.store(account2) acc2Stored = await ocean.auth.isStored(account2) assert.isTrue(acc2Stored) + assert.isTrue(await account2.isTokenStored()) }) }) diff --git a/src/ocean/Account.ts b/src/ocean/Account.ts index faf09f1..398b832 100644 --- a/src/ocean/Account.ts +++ b/src/ocean/Account.ts @@ -51,10 +51,18 @@ export default class Account extends Instantiable { /** * Returns account token. - * @return {string} Account token. + * @return {Promise} Account token. */ - public getToken(): string { - return this.token + public async getToken(): Promise { + return this.token || await this.ocean.auth.restore(this) + } + + /** + * Returns if account token is stored. + * @return {Promise} Is stored. + */ + public isTokenStored(): Promise { + return this.ocean.auth.isStored(this) } /** diff --git a/src/ocean/OceanAccounts.ts b/src/ocean/OceanAccounts.ts index df80368..19a274b 100644 --- a/src/ocean/OceanAccounts.ts +++ b/src/ocean/OceanAccounts.ts @@ -29,13 +29,6 @@ export class OceanAccounts extends Instantiable { const accountPromises = ethAccounts .map(address => new Account(address, this.instanceConfig)) - .map(async account => { - const token = await this.ocean.auth.restore(account) - if (token) { - account.setToken(token) - } - return account - }) return Promise.all(accountPromises) }