workaround for getting the token in the web worker

This commit is contained in:
Blair Vanderlugt 2021-08-02 11:50:20 -07:00
parent 858e1e65bd
commit c03745e896
2 changed files with 32 additions and 22 deletions

View File

@ -41,7 +41,7 @@ export class UserService {
constructor(
private httpClient: HttpClient,
private loggingService: LoggingService,
private tokenService: TokenService
private tokenService: TokenService,
) {}
async init(): Promise<void> {
@ -76,7 +76,7 @@ export class UserService {
userLocation: string,
location: string,
locationType: string,
oldPhoneNumber: string
oldPhoneNumber: string,
): Promise<any> {
const accountInfo: any = {
vcard: {
@ -137,11 +137,11 @@ export class UserService {
this.loggingService.sendErrorLevelMessage(
'Cannot find account info in meta service',
this,
{ error }
{ error },
);
const syncableAccount: Syncable = new Syncable(accountKey, accountInfo);
await this.updateMeta(syncableAccount, accountKey, this.headers);
}
},
);
if (phoneNumber !== oldPhoneNumber) {
const oldPhoneKey: string = await Phone.toKey(oldPhoneNumber);
@ -157,7 +157,7 @@ export class UserService {
async updateMeta(
syncableAccount: Syncable,
accountKey: string,
headers: HttpHeaders
headers: HttpHeaders,
): Promise<any> {
const envelope: Envelope = await this.wrap(syncableAccount, this.signer);
const reqBody: string = envelope.toJSON();
@ -214,22 +214,25 @@ 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(
'Web workers are not supported in this environment'
'Web workers are not supported in this environment',
);
for (const accountAddress of accountAddresses.slice(offset, offset + limit)) {
await this.getAccountByAddress(accountAddress, limit);
@ -243,7 +246,7 @@ export class UserService {
async getAccountByAddress(
accountAddress: string,
limit: number = 100
limit: number = 100,
): Promise<Observable<AccountDetails>> {
const accountSubject: Subject<any> = new Subject<any>();
this.getAccountDetailsFromMeta(await User.toKey(add0x(accountAddress)))
@ -255,7 +258,7 @@ export class UserService {
this.tokenService.load.subscribe(async (status: boolean) => {
if (status) {
accountInfo.balance = await this.tokenService.getTokenBalance(
accountInfo.identities.evm[`bloxberg:${environment.bloxbergChainId}`][0]
accountInfo.identities.evm[`bloxberg:${environment.bloxbergChainId}`][0],
);
}
});
@ -269,7 +272,7 @@ export class UserService {
async getAccountByPhone(
phoneNumber: string,
limit: number = 100
limit: number = 100,
): Promise<Observable<AccountDetails>> {
const accountSubject: Subject<any> = new Subject<any>();
this.getAccountDetailsFromMeta(await Phone.toKey(phoneNumber))
@ -301,7 +304,7 @@ export class UserService {
const keywords = product.toLowerCase().split(' ');
for (const keyword of keywords) {
const queriedCategory: string = Object.keys(categories).find((key) =>
categories[key].includes(keyword)
categories[key].includes(keyword),
);
if (queriedCategory) {
return queriedCategory;
@ -326,7 +329,7 @@ export class UserService {
const savedIndex = this.accounts.findIndex(
(acc) =>
acc.identities.evm[`bloxberg:${environment.bloxbergChainId}`][0] ===
account.identities.evm[`bloxberg:${environment.bloxbergChainId}`][0]
account.identities.evm[`bloxberg:${environment.bloxbergChainId}`][0],
);
if (savedIndex === 0) {
return;

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) {