Skip to content

Commit

Permalink
fix: avoid refreshing undefined token
Browse files Browse the repository at this point in the history
  • Loading branch information
Harasz committed Apr 20, 2022
1 parent a7873ff commit 86bed3c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
23 changes: 21 additions & 2 deletions docs/api/classes/modules_assets.AssetsService.md
Expand Up @@ -14,6 +14,7 @@
- [cancelAssetOffer](modules_assets.AssetsService.md#cancelassetoffer)
- [getAssetById](modules_assets.AssetsService.md#getassetbyid)
- [getAssetHistory](modules_assets.AssetsService.md#getassethistory)
- [getAssetOwner](modules_assets.AssetsService.md#getassetowner)
- [getOfferedAssets](modules_assets.AssetsService.md#getofferedassets)
- [getOwnedAssets](modules_assets.AssetsService.md#getownedassets)
- [getPreviouslyOwnedAssets](modules_assets.AssetsService.md#getpreviouslyownedassets)
Expand Down Expand Up @@ -86,7 +87,7 @@ ___

| Name | Type | Description |
| :------ | :------ | :------ |
| `id` | `Object` | Asset Id |
| `id` | `Object` | Asset decentralized identifier |
| `id.id` | `string` | - |

#### Returns
Expand Down Expand Up @@ -122,6 +123,24 @@ Asset[] || []

___

### getAssetOwner

**getAssetOwner**(`id`): `Promise`<`string`\>

Returns owner of the given asset

#### Parameters

| Name | Type | Description |
| :------ | :------ | :------ |
| `id` | `string` | asset decentralized identifier |

#### Returns

`Promise`<`string`\>

___

### getOfferedAssets

**getOfferedAssets**(`__namedParameters?`): `Promise`<[`Asset`](../interfaces/modules_assets.Asset.md)[]\>
Expand Down Expand Up @@ -223,7 +242,7 @@ ___

`Promise`<`string`\>

Asset DID
Asset address

___

Expand Down
34 changes: 21 additions & 13 deletions src/modules/cache-client/cache-client.service.ts
Expand Up @@ -63,15 +63,18 @@ export class CacheClient implements ICacheClient {
*/
async authenticate() {
try {
const { refreshToken, token } = await this.refreshToken();
if (!this.isBrowser) {
this.httpClient.defaults.headers.common[
'Authorization'
] = `Bearer ${token}`;
}
if (await this.isAuthenticated()) {
this.refresh_token = refreshToken;
return;
const refreshedTokens = await this.refreshToken();

if (refreshedTokens) {
if (!this.isBrowser) {
this.httpClient.defaults.headers.common[
'Authorization'
] = `Bearer ${refreshedTokens.token}`;
}
if (await this.isAuthenticated()) {
this.refresh_token = refreshedTokens.refreshToken;
return;
}
}
} catch {
// Ignore errors
Expand Down Expand Up @@ -384,10 +387,15 @@ export class CacheClient implements ICacheClient {
return data;
}

private async refreshToken(): Promise<{
token: string;
refreshToken: string;
}> {
private async refreshToken(): Promise<
| {
token: string;
refreshToken: string;
}
| undefined
> {
if (!this.refresh_token) return undefined;

const { data } = await this.httpClient.get<{
token: string;
refreshToken: string;
Expand Down

0 comments on commit 86bed3c

Please sign in to comment.