Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid refreshing undefined token #532

Merged
merged 1 commit into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions docs/api/classes/modules_assets.AssetsService.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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