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

View File

@ -8,6 +8,7 @@ import * as vCard from 'vcard-parser';
const headers = { const headers = {
'x-cic-automerge': 'client', 'x-cic-automerge': 'client',
}; };
const options = { const options = {
headers, headers,
}; };
@ -16,17 +17,23 @@ addEventListener('message', async ({ data }) => {
if (data.addresses instanceof Array) { if (data.addresses instanceof Array) {
for (const accountAddress of data.addresses) { for (const accountAddress of data.addresses) {
try { try {
const account = await getAccountByAddress(accountAddress, data.url); const account = await getAccountByAddress(accountAddress, data.url, data.token);
postMessage(account); postMessage(account);
} catch (error) { } 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)); const userKey = await User.toKey(add0x(accountAddress));
headers['Authorization'] = 'Bearer ' + token;
const response = await fetch(`${metaUrl}/${userKey}`, options) const response = await fetch(`${metaUrl}/${userKey}`, options)
.then((res) => { .then((res) => {
if (res.ok) { if (res.ok) {