Merge branch 'bvander/worker-thread-auth-hack' into 'master'

workaround for getting the token in the web worker

See merge request grassrootseconomics/cic-staff-client!45
This commit is contained in:
Blair Vanderlugt 2021-08-03 17:51:12 +00:00
commit 742c589835
3 changed files with 22 additions and 12 deletions

View File

@ -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';

View File

@ -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(

View File

@ -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<any> {
async function getAccountByAddress(
accountAddress: string,
metaUrl: string,
token: string,
): Promise<any> {
const userKey = await User.toKey(add0x(accountAddress));
headers['Authorization'] = 'Bearer ' + token;
const response = await fetch(`${metaUrl}/${userKey}`, options)
.then((res) => {
if (res.ok) {