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

Improve how can be get the token from the account.

This commit is contained in:
Pedro Gutiérrez 2019-05-09 16:34:44 +02:00
parent 9dc4b348ce
commit 1f83e333cb
3 changed files with 18 additions and 10 deletions

View File

@ -38,7 +38,11 @@ describe("Authentication Token", () => {
}) })
it("should store the token for a user", async () => { it("should store the token for a user", async () => {
assert.isUndefined(await account1.getToken())
await ocean.auth.store(account1) 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 () => { it("should restore the token for a user", async () => {
@ -60,11 +64,14 @@ describe("Authentication Token", () => {
acc2Stored = await ocean.auth.isStored(account2) acc2Stored = await ocean.auth.isStored(account2)
assert.isTrue(acc1Stored) assert.isTrue(acc1Stored)
assert.isTrue(await account1.isTokenStored())
assert.isFalse(acc2Stored) assert.isFalse(acc2Stored)
assert.isFalse(await account2.isTokenStored())
await ocean.auth.store(account2) await ocean.auth.store(account2)
acc2Stored = await ocean.auth.isStored(account2) acc2Stored = await ocean.auth.isStored(account2)
assert.isTrue(acc2Stored) assert.isTrue(acc2Stored)
assert.isTrue(await account2.isTokenStored())
}) })
}) })

View File

@ -51,10 +51,18 @@ export default class Account extends Instantiable {
/** /**
* Returns account token. * Returns account token.
* @return {string} Account token. * @return {Promise<string>} Account token.
*/ */
public getToken(): string { public async getToken(): Promise<string> {
return this.token return this.token || await this.ocean.auth.restore(this)
}
/**
* Returns if account token is stored.
* @return {Promise<boolean>} Is stored.
*/
public isTokenStored(): Promise<boolean> {
return this.ocean.auth.isStored(this)
} }
/** /**

View File

@ -29,13 +29,6 @@ export class OceanAccounts extends Instantiable {
const accountPromises = ethAccounts const accountPromises = ethAccounts
.map(address => new Account(address, this.instanceConfig)) .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) return Promise.all(accountPromises)
} }