From 7ae04139911ddac08dee87db36dfb2f61d4a480a Mon Sep 17 00:00:00 2001 From: Blair Vanderlugt Date: Tue, 3 Aug 2021 17:51:11 +0000 Subject: [PATCH] workaround for getting the token in the web worker --- src/app/_helpers/index.ts | 2 +- src/app/_services/user.service.ts | 19 +++++++++++-------- src/app/_workers/fetch-accounts.worker.ts | 13 ++++++++++--- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/app/_helpers/index.ts b/src/app/_helpers/index.ts index 1bdb553..3adb859 100644 --- a/src/app/_helpers/index.ts +++ b/src/app/_helpers/index.ts @@ -10,4 +10,4 @@ export * from '@app/_helpers/read-csv'; export * from '@app/_helpers/schema-validation'; export * from '@app/_helpers/sync'; export * from '@app/_helpers/online-status'; -export * from './to-hex'; +export * from '@app/_helpers/to-hex'; diff --git a/src/app/_services/user.service.ts b/src/app/_services/user.service.ts index 4f04416..49c99f4 100644 --- a/src/app/_services/user.service.ts +++ b/src/app/_services/user.service.ts @@ -219,18 +219,21 @@ export class UserService { if (typeof Worker !== 'undefined') { const worker = new Worker('@app/_workers/fetch-accounts.worker', { type: 'module' }); worker.onmessage = ({ data }) => { - this.tokenService.load.subscribe(async (status: boolean) => { - if (status) { - data.balance = await this.tokenService.getTokenBalance( - data.identities.evm[`bloxberg:${environment.bloxbergChainId}`][0] - ); - } - }); - this.addAccount(data, limit); + if (data) { + this.tokenService.load.subscribe(async (status: boolean) => { + if (status) { + data.balance = await this.tokenService.getTokenBalance( + data.identities.evm[`bloxberg:${environment.bloxbergChainId}`][0] + ); + } + }); + this.addAccount(data, limit); + } }; worker.postMessage({ addresses: accountAddresses.slice(offset, offset + limit), url: environment.cicMetaUrl, + token: sessionStorage.getItem(btoa('CICADA_SESSION_TOKEN')), }); } else { this.loggingService.sendInfoLevelMessage( diff --git a/src/app/_workers/fetch-accounts.worker.ts b/src/app/_workers/fetch-accounts.worker.ts index 519b760..078aa80 100644 --- a/src/app/_workers/fetch-accounts.worker.ts +++ b/src/app/_workers/fetch-accounts.worker.ts @@ -8,6 +8,7 @@ import * as vCard from 'vcard-parser'; const headers = { 'x-cic-automerge': 'client', }; + const options = { headers, }; @@ -16,17 +17,23 @@ addEventListener('message', async ({ data }) => { if (data.addresses instanceof Array) { for (const accountAddress of data.addresses) { try { - const account = await getAccountByAddress(accountAddress, data.url); + const account = await getAccountByAddress(accountAddress, data.url, data.token); postMessage(account); } catch (error) { - throw Error(error); + console.log(`ERROR we failed to get account ${accountAddress}`, error); } } } }); -async function getAccountByAddress(accountAddress: string, metaUrl: string): Promise { +async function getAccountByAddress( + accountAddress: string, + metaUrl: string, + token: string, +): Promise { const userKey = await User.toKey(add0x(accountAddress)); + + headers['Authorization'] = 'Bearer ' + token; const response = await fetch(`${metaUrl}/${userKey}`, options) .then((res) => { if (res.ok) {